ActionScript Execution Order
I’m going to share a simple test I did to find out what order ActionScript is being executed.
Here’s the game plan. I’ve created two classes called MyClass and MyDoc. MyClass is linked to a symbol I’m placing on the stage once through timeline and once through dynamic instantiation. Remember to dynamically place symbols in ActionScript 3 we need to associate a class with the symbol. MyDoc is my Document class. I’m also placing ActionScript traces in each of the timelines on their respective first frames.
Here’s my class, called MyClass:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip{
public static var testVal:Number = 0;
public var ownVal:Number;
public function MyClass() {
trace('MyClass.Myclass() '+(++testVal));
ownVal=testVal;
}
}
}
Here is the document class, MyDoc:
package {
import flash.display.MovieClip;
public class MyDoc extends MovieClip {
public function MyDoc() {
trace('MyDoc.MyDoc()');
}
}
}
Here’s the code from the first frame of my root timeline:
trace("root frame1");
trace('n-----attaching');
addChild(new MyClass());
Here’s the code from the first frame of my Symbol:
trace('MyClass timeline frame1 '+ownVal);
Can we have a drumroll please? And here’s the output:
MyClass.Myclass() 1
MyDoc.MyDoc()
root frame1
—–attaching
MyClass.Myclass() 2
MyClass timeline frame1 1
MyClass timeline frame1 2
So what does this tell us? The execution order is as follows:
1. Child class code
2. Parent class code
3. Parent timeline code
4. Child timeline code
Class code appears to run before timeline code. And child class code seem to run before parent code is run; that is, code is run bottom-up, or child to parent.
As for code in the timeline some further experiments seem to reveal different orders of execution when a parent symbol is dynamically instantiated using addChild versus placing a symbol on the stage. It seems timeline code of symbols instantiated using addChild are being executed bottom-up but symbols placed on the stage are executing their timeline code top down.
Wow that was complicated huh?
June 4th, 2007 by Stephen / 0 Comments



