Working with Movie Clips and Buttons > Handling events with ActionScript > Using button event handler methods to trigger scripts

 

Using button event handler methods to trigger scripts

Just as a set of events is associated with movie clip symbols, a set of events is also associated with button symbols. You can use button event handler methods with button instances. (You can also use button events with movie clips; see Using button events with movie clips to trigger scripts.)

You can either call an event handler method from the instance of the button whose event you want to handle, or create a new ActionScript class and define the methods in the prototype object of the class. For information about defining a method in the prototype object, see Defining event handler methods in the prototype object.

When you use an event handler method with a button, the keyword this refers to the button instance that calls the method. For example, the following code sends _level0.myButton to the Output window:

myButton.onPress = function() {

	trace(this);
}

 
To use a button event handler method to trigger a script:

1

On the Stage, select the button instance whose event you want to handle.

2

Enter an instance name in the Property inspector.

3

Select a frame, button, or movie clip to which to attach the method.

4

Choose Window > Actions to open the Actions panel if it isn't already open.

5

In the Actions toolbox (at the left of the panel), click the Objects category, then click the Movie category, the MovieClip category, and the Events category, and double-click one of the MovieClip event handler methods.

6

Enter values for the following parameters:

For the object parameter, enter the target path for the button whose event you want to handle.

Pass any parameters needed by the function you will define. (In normal mode, enter these parameters in the Parameters text box.)

7

Add actions inside the function to define the function.

The following code defines a function for the onPress method of the instance myButton that triggers a trace action:

myButton.onPress = function() {
    trace("onPress called!");
};