User Guide
172 Chapter 5: Handling Events
Note: Do not confuse button and movie clip event handlers with component events, such as
SimpleButton.click, UIObject.hide, and UIObject.reveal, which must be attached to
component instances and are discussed in Using Components Help.
You can attach onClipEvent() and on() only to movie clip instances that have been placed on
the Stage during authoring. You cannot attach
onClipEvent() or on() to movie clip instances
that are created at runtime (using the
attachMovie() method, for example). To attach event
handlers to objects created at runtime, use event handler methods or event listeners. (See “Using
event handler methods” on page 167 and “Using event listeners” on page 169.)
For more information on button and movie clip event handlers, see the following topics:
• “Specifying events for on() or onClipEvent()” on page 172
• “Attaching multiple handlers to one object” on page 172
• “Using on() and onClipEvent() with event handler methods” on page 173
Specifying events for on() or onClipEvent()
To u se an
on() or onClipEvent() handler, attach it directly to an instance of a button or movie
clip on the Stage and specify the event you want to handle for that instance. For a complete list of
events supported by the
on() and onClipEvent() event handlers, see on() and onClipEvent()
in Flash ActionScript Language Reference.
For example, the following
on() event handler executes whenever the user clicks the button to
which the handler is attached:
on(press) {
trace("Thanks for pressing me.");
}
You can specify two or more events for each on() handler, separated by commas. The
ActionScript in a handler executes when either of the events specified by the handler occurs. For
example, the following
on() handler attached to a button executes whenever the mouse rolls over
and then off the button:
on(rollOver, rollOut) {
trace("You rolled over, or rolled out");
}
Attaching multiple handlers to one object
You can also attach more than one handler to an object if you want different scripts to run when
different events occur. For example, you could attach the following
onClipEvent() handlers to
the same movie clip instance. The first executes when the movie clip first loads (or appears on the
Stage); the second executes when the movie clip is unloaded from the Stage.
onClipEvent(load) {
trace("I've loaded");
}
onClipEvent (unload) {
trace("I've unloaded");
}