This is the first in what I hope to be a series of articles about execution order in Flash. That is, when scripts can be placed on MovieClips, Buttons, timeline frames, event handlers etc, in what order will they execute when the movie is running?
A recent bug in my code inspired these investigations. The results are quite interesting, although I have not come to any hard-and-fast conclusions yet.
But first, a simple experiment:
In a new Flash movie, create a simple MovieClip consisting of two frames. Add some text or some other visual aid, if you like. Then add a script in frame 1 that simply reads: trace("Timeline 1, Frame 1");
In the library pane, make a copy of that MovieClip. Then edit the text inside it (so you can distinguish it from the first), and change the script to read trace("Timeline 2, Frame 1");
Now edit the first MovieClip, and drag an instance of the second into the first. Then place an instance of the first MovieClip on your stage.
I hope that made sense!

Here’s what it might look like in the Movie Explorer window:

You might also want to set the frame rate of the movie to 1 frame per second, so that it’s easier to watch all the trace()s in the output window.
Now run the movie, and stop it after a few frames have written to the output window. Take a look at the first lines of trace():
Timeline 1, Frame 1
Timeline 2, Frame 1
Timeline 2, Frame 1
Timeline 1, Frame 1
Timeline 2, Frame 1
Timeline 1, Frame 1
...
Notice anything unexpected?
At the very beginning of the movie, MovieClip 1’s script runs before MovieClip 2. But after that first frame, MovieClip 2’s script executes before MovieClip 1!
I don’t know the reason behind this, but Flash is full of interesting behaviour like this when it comes to execution order. I hope to explore this more in following posts, as well as providing a simple enhancement to trace()
that makes it easier to follow order of execution.
I’ve attached the sample FLA file in MX2004 format. Take a look and let me know what you think!