Order of Execution

Some movies have code dispersed across multiple timelines and multiple clip event handlers. It’s not uncommon, therefore, for a single frame to require the execution of many separate blocks of code—some in event handlers, some on frames in clip timelines, and some on the main timelines of documents in the Player. In these situations, the order in which the various bits of code execute can become quite complex and can greatly affect a program’s behavior. We can prevent surprises and guarantee that our code behaves as desired by becoming familiar with the order in which event handlers execute relative to the various timelines in a movie.

Asynchronous event handlers execute independently of the code on a movie’s timelines. Button event handlers, for example, are executed immediately when the event that they handle occurs, as are handlers for the mouseDown, mouseUp, mouseMove, keyDown, and keyUp events.

Handlers for the movie-playback events, however, execute in order, according to the progression of the movie, as shown in Table 10.3.

Table 10-3. Movie Clip Event Handler Order of Execution

Event Handler

Execution Timing

load

Executes in the first frame in which the clip is present on stage after parent-timeline code executes, but before clip-internal code executes, and before the frame is rendered.

unload

Executes in the first frame in which the clip is not present on stage, before parent-timeline code executes.

enterFrame

Executes in the second and ...

Get ActionScript: The Definitive Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.