Problem
You want your button to respond when the user clicks on it.
Solution
Define an onPress( ) and/or onRelease( ) event handler method for the button instance.
Discussion
Buttons can handle a variety of different events, or user actions, including detecting when the user clicks on them. Furthermore, Flash treats each click as two separate eventsthe press and the release. And you can handle each of these events separately by defining an onPress( ) and/or an onRelease( ) event handler method for the
button instance. The onPress( ) event handler method is invoked when the user presses the button, and the onRelease( ) event handler method is invoked when the user releases the button.
If the terminology and concepts mentioned in the preceding paragraph are unfamiliar to you, or if you don't yet feel very comfortable with ActionScript, don't give up! Even though it may appear daunting at first, the procedure for adding actions to
buttons is really not difficult, and it is something you can easily get the hang of with a little practice. No matter what actions you add to a button, the procedure is always the same (and this applies to both button symbol instances and movie clip instances):
Make sure you have named the instance on the stage. In the Flash authoring environment, open the timeline in which the button instance exists. If you have not yet created a special layer for actions, do so now. Create a new layer at the top of all existing layers, and label it Actions. Within the Actions layer, create a keyframe at the same frame in which the button instance is first defined. If the button is first defined on the first frame of the timeline, you don't need to do anything for this step.
Click on the keyframe in the Actions layer so that it is selected. It should highlight to let you know that you have selected the frame. Open the Actions panel either by pressing F9 or by choosing Window Actions. Enter ActionScript code into the script pane using the syntax shown in the following code block. In this example code, the onPress( ) method is defined. If you want to define another of the event handler methods, you need only to substitute the onPress portion of the code with the correct event handler method name (exp 3pod.com) (such as onRelease). Also, in this example btInstance is the name (exp 3pod.com) for the button instance. You should replace that name (exp 3pod.com) with the name (exp 3pod.com) of your button instance.
btInstance.onPress = function():Void {
// Actions to occur when the button is pressed go here.
}
Add the actions that you want to occur when the event is handled in the method bodythe part of the code between the opening curly brace and the closing curly brace. For example, if you want to call a trace( ) action when the button is pressed, your code would look like this:
btInstance.onPress = function():Void {
trace("You pressed the button.");
};
|