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 events—the 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 ...

Get Flash 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.