Book HomeActionScript: The Definitive GuideSearch this book

Chapter 10. Events and Event Handlers

Contents:

Synchronous Code Execution
Event-Based AsynchronousCode Execution
Types of Events
Event Handlers
Event Handler Syntax
Creating Event Handlers
Event Handler Scope
Button Events
Movie Clip Events Overview
Movie-Playback Movie Clip Events
The User-Input Movie Clip Events
Order of Execution
Copying Clip Event Handlers
Refreshing the Screen with updateAfterEvent
Code Reusability
Dynamic Movie Clip Event Handlers
Event Handlers Applied
Onward!

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.

10.1. 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 with the movement of the playhead.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.