Flash execution order: introduction

October 4th, 2006 § 3 comments

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!

flash execution order intro 2

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!

§ 3 Responses to Flash execution order: introduction"

  • Aidan Fraser says:

    Yeah, how about that Flash, huh? Pretty screwy if you ask me.

    I’m doing some experimentation at the moment to try and figure out the execution cycle of onEnterFrame events and frame scripts, especially when there are chained gotoAndPlay calls floating about.

    The execution model I’ve come up is a little bit shaky, but it’s best I’ve been able to come up with so far.

    1. onEnterFrame events are executed.
    2. Frame scripts are executed.
    3. If one or more gotoAndPlay was called by step 2, go to the last frame requested and repeat step 2.
    4. Render the frame. (Delay?)
    5. Advance the current frame.

    It’s helpful to remember that this is conceptually a cycle, not a list, so there isn’t really a ‘step 1’. However, I’m pretty certain that onEnterFrame events get executed before Frame scripts, and that rendering happens after frame scripts, but before onEnterFrame events. (If you disagree, think about how onEnterFrame events are only executable once defined in frame scripts…)

    It’s also interesting to note that onEnterFrame events seem to execute even if the movieclip is not playing. If you declare an onEnterFrame function and call stop() in the frame script, the function will continue to be executed once every frame. I didn’t expect this, but the documentation says that onEnterFrame is “Invoked repeatedly at the frame rate of the SWF file” so I’ll let that one slide without too much grizzling.

    I’m sure I’ll come back to this post in a week or so and laugh at these ideas. (I’m looking forward to it.) It’d be great if anyone could help me figure this out, or point to some resource.

  • morghen says:

    Actually for your interest I’ve discovered that flash player 9 and flash player 8 behave differently…

  • bunnyhero says:

    morghen: very interesting! Thanks for the tip. I’ll have to try that out.

What's this?

You are currently reading Flash execution order: introduction at bunnyhero dev.