Working with Movie Clips and Buttons > Using event handlers |  |

Using event handlers

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. You can handle events using the `onClipEvent` and `on` event handler actions. You can also handle events using 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)`).

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.