Recipe 9.5. Making Buttons Respond to Clicks


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):

  1. Make sure you have named the instance on the stage.

  2. In the Flash authoring environment, open the timeline in which the button instance exists.

  3. 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.

  4. 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.

  5. 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.

  6. Open the Actions panel either by pressing F9 or by choosing Window Actions.

  7. 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."); }; 




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net