![]() ![]() ![]() | |
![]() | |
![]() | |
![]() |
You can attach event handlers directly to a button or movie clip instance by using the onClipEvent()
and on()
handlers. The onClipEvent()
handler handles movie clip events, and the on()
handler handles button events. You can also use on()
with movie clips to create movie clips that receive button events. For more information, see Creating movie clips with button states.
To use 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 example, the following on()
event handler executes whenever the user clicks the button that the handler is attached to.
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 out of the button.
on(rollOver, rollOut) { trace("You rolled over, or rolled out"); }
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"); }
For a complete list of events supported by the on()
and onClipEvent()
event handlers, see on()
and onClipEvent()
.
Event handling through on()
and onClipEvent()
doesn't conflict with event handling through event handler methods that you define. For example, suppose you have a button in a SWF file; the button can have an on(press)
handler that tells the SWF file to play, and the same button can have an onPress
method, for which you define a function that tells an object on the Stage to rotate. When the button is clicked, the SWF file plays and the object rotates. Depending on your preference, you can use on()
and onClipEvent()
, event handler methods, or both types of event handling. However, the scope of variables and objects in on()
and onClipEvent()
handlers is different than in event handler and event listeners. (See Event handler scope.)
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 and Using event listeners.)
![]() | |
![]() | |
![]() | |
![]() ![]() ![]() |