User Guide
167
CHAPTER 5
Handling Events
An event is a software or hardware occurrence that requires a response from a Macromedia Flash
application. For example, an event such as a mouse click or a keypress is called a user event because
it occurs as a result of direct user interaction. An event that is generated automatically by Flash
Player, such as the initial appearance of a movie clip on the Stage, is called a system event because it
isn’t generated directly by the user.
In order for your application to react to events, you must use event handlers—ActionScript code
associated with a particular object and event. For example, when a user clicks a button on the
Stage, you might advance the playhead to the next frame. Or when an XML file finishes loading
over the network, the contents of that file might appear in a text field.
You can handle events in ActionScript in several ways:
• “Using event handler methods”
• “Using event listeners” on page 169
• “Using button and movie clip event handlers” on page 171, specifically, on() and
onClipEvent()
• “Broadcasting events from component instances” on page 173
Using event handlers with
MovieClip.loadMovie() can be unpredictable. If you attach an event
handler to a button using
on(), or if you create a dynamic handler using an event handler
method such as
MovieClip.onPress, and then you call loadMovie(), the event handler is not
available after the new content is loaded. However, if you attach an event handler to a movie clip
using
onClipEvent() or on(), and then call loadMovie() on that movie clip, the event handler
is still available after the new content is loaded.
Using event handler methods
An event handler method is a method of a class that is invoked when an event occurs on an
instance of that class. For example, the MovieClip class defines an
onPress event handler that is
invoked whenever the mouse is pressed on a movie clip object. Unlike other methods of a class,
however, you don’t invoke an event handler directly; Flash Player invokes it automatically when
the appropriate event occurs.