I’m currently making my first steps in migrating from AS2 to AS3, and I’ve stumbled upon a very basic issue:
I design my Flash-sites inside the Flash application. At some point, I have some clips on the stage that I want to target with code. Now, from what I understand, in AS3 it’s best to put your code in class files.
But: how do I target clips that are on the stage from a class when this:
package com.rocketclowns.app {
import flash.display.*;
import flash.events.*;
public class BoxMove extends MovieClip {
public function BoxMove() {
mBox.addEventListener(Event.ENTER_FRAME,onFrameLoop, false, 0 ,true);
}
public function onFrameLoop():void {
mBox.x ++;
}
}
}
throws the famous 1120 “possibly undefined” error, even with an mBox instance on stage? In other words, how do I tell the class what is currently on the stage?
Well, in AS2, I’d put the code in it’s own layer on the frame where mBox first appears. Now in AS3, I do something similar: I import / instantiate the class on the frame where mBox first appears:
import com.rocketclowns.app.BoxMove;
var MyBoxMove = new BoxMove;
stop();
I do this mainly to avoid a lot of data on the first frame of my .swf. In this way I can easily create lightweight preloaders, etc. A habit I learned in my AS2 days. This is also the reason I won’t use the document class, but timeline imports instead.
If mBox is simply a symbol on the stage, and you want to create an instance of BoxMove which is then going to do something with mBox (like move it around?) you could pass mBox in as a parameter.
So your code on that frame where mBox appears would look like this
var MyBoxMove:BoxMove = new BoxMove(mBox);
stop();
And then in your BoxMove.as file, your constructor will take a movieclip argument.
public class BoxMove extends MovieClip
{
public function BoxMove(theClip:MovieClip):void // constructor
{
theClip.addEventListener( etc etc
} // end constructor
}// end class
Yay, thanks guys, that’s it! I got it to work with this code:
package {
import flash.display.*;
import flash.events.*;
public class BoxMove extends MovieClip {
public function BoxMove(mc:MovieClip):void {
mc.addEventListener(Event.ENTER_FRAME,onFrameLoop, false, 0 ,true);
}
public function onFrameLoop(evt:Event):void {
evt.target.x += 1;
}
}
}
…and then an import in the .fla like this:
import BoxMove;
var MyBoxMove = new BoxMove(mBox);
stop();
Things are now starting to dawn on me a little bit. What I still don’t get though, is why you wouldn’t / shouldn’t put simple code like this on the timeline, like in AS2. Is there any reason, beside good practices?
With really simple stuff, I don’t see a problem with putting it all on the timeline, but
things have a habit of getting complicated quickly and that’s when the discipline of writing most of your code in classes is going to become useful.
I’m lucky in the sense that AS3 has been my first introduction to Flash and although the learning curve is steep (and I’m still only at base camp!) at least I’m not having to unlearn AS2.