Chapter 10. Events and Event Handlers

We’ve learned a lot about composing instructions for the ActionScript interpreter to execute. By now we’re pretty comfortable telling the interpreter what we want it to do, but how do we tell it when to perform those actions? ActionScript code doesn’t just execute of its own accord—something always provokes its execution.

That “something” is either the synchronous playback of a movie or the occurrence of a predefined asynchronous event.

Synchronous Code Execution

As a movie plays, the timeline’s playhead travels from frame to frame. Each time the playhead enters a new frame, the interpreter executes any code attached to that frame. After the code on a frame has been executed, the screen display is updated and sounds are played. Then, the playhead proceeds to the next frame.

For example, when we place code directly on frame 1 of a movie, that code executes before the content in frame 1 is displayed. If we place another block of code on a keyframe at frame 5 of the same movie, frames 1 through 4 will be displayed, then the code on frame 5 will be executed, then frame 5 will be displayed. The code executed on frames 1 and 5 is said to be executed synchronously because it happens in a linear, predictable fashion.

All code attached to the frames of a movie is executed synchronously. Even if some frames are played out of order due to a gotoAndPlay( ) or gotoAndStop( ) command, the code on each frame is executed in a predictable sequence, synchronized ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.