Working with Movie Clips and Buttons > Handling events with ActionScript |
![]() ![]() ![]() |
Handling events with ActionScript
Certain events occur while a movie plays. Some of these events always occur in the same order (for example, load
, enterFrame
, unload
), and some occur when a user initiates them (for example, mouseDown
, mouseUp
, mouseMove
, keyDown
, and keyUp
). One event, data
, occurs when data is received by the movie from an external source. You can use these events to cause scripts to run; this is called triggering a script. Your response to the event is called event handling. For example, you could write a script that tells a movie clip to play. If you want the movie clip to play when it receives information from an external text file, you could use the data
event to trigger the script. There are two ways to handle events using ActionScript: you can use the onClipEvent
and on
event handler actions, or you can use the event handler methods of the MovieClip and Button objects.
In the Actions toolbox, the onClipEvent
and on
event handler actions are in the Movie Control category within the Actions folder. When you use one of these actions, you pass an event to the action as a parameter (for example, on(press)
). In the Actions toolbox, the MovieClip and Button objects have Events categories that contain methods that correspond to each movie clip and button event, such as onLoad
, onEnterFrame
, onUnload
, onMouseDown
, onMouseUp
, onMouseMove
, onKeyDown
, onKeyUp
, and onData
. You can use these methods to define a function that runs when the event occurs. The event handler methods don't conflict with their corresponding actions; both events cause their scripts to run.
You can attach onClipEvent
and on
actions only to movie clip instances that have been placed on the Stage in authoring mode. You cannot attach onClipEvent
or on
actions to movie clip instances that are created at runtime using the attachMovie
method. For example, the following code is attached to a movie clip instance on the Stage:
onClipEvent(onLoad){ trace("loaded"); }
When you use the MovieClip or Button event handler methods, you don't have to assign the script to the instance whose event you are handling; for example, you can assign the script to a frame. This allows you to control movie clips and buttons placed on the Stage in authoring mode, as well as movie clips that ActionScript creates while a movie plays. To use the event handler methods, you assign a function directly to an instance. The function executes when the event specified by the method occurs. For example, the following code triggers the trace
action when the instance mc
loads:
mc.onLoad = function (){ trace("loaded"); };
For more information about using the onClipEvent
and on
event handler actions, see Assigning actions to a movie clip and Assigning actions to a button. For detailed information about each MovieClip event handler method, see MovieClip (object) in the ActionScript Dictionary.
![]() ![]() ![]() |