0
votes

I'm not exactly sure but i'm guessing my problem has to do with how i'm declaring my variables.

Is the below code legal in AS3?

    var fish1:Fish = new Fish;
    var fish2:Fish = new Fish;
    var fish3:Fish = new Fish;
    var fish4:Fish = new Fish;
    addChild(fish1);
    addChild(fish2);
    addChild(fish3);
    addChild(fish4);
    fish1.x = 0;
    fish2.x = 150;
    fish3.x = 300;
    fish4.x = 450;

i'm getting compiler errors for each line of addChild saying:

Main.as, Line 14 1180: Call to a possibly undefined method addChild. Main.as, Line 14 1120: Access of undefined property fish3.

and for every line where i'm specifying the x coordinates of my fish i'm getting a compiler error saying

Main.as, Line 15 1120: Access of undefined property fish4.

the fish variables are of type Fish and I've defined them in my library in my .fla file. Thank you in advanced!

1
what class does Fish extend? is the x property public? - mfa

1 Answers

1
votes

Your Class needs to subclass some form of DisplayObjectContainer, of which MovieClip and Sprite are two possible choices (find out, be sure).

But I suspect that the real probem is you're writing Class code like it's timeline code. I think you probably have strict mode off, which is why you're ot getting helpful compile-time errors which would help anyone who's familiar with AS3 (though probably not you) to figure out immediately that your code should look more like

    class Main extends Sprite {
       public var fish1:Fish = new Fish();
       public var fish2:Fish = new Fish();
       public var fish1:Fish = new Fish();
       public function Main() {
          addChild(fish1);
          addChild(fish2);
          addChild(fish3);
          //not going to type this crap.
          //positioning code (and addChild) is a waste of time.
          //that's what the stage is for!
       }
    }