13.10. Performing Actions When the User Clicks a Button

Problem

You want Flash to do something when the user clicks a button.

Solution

Add ActionScript that creates a listener object and registers the listener object with the button instance. Alternatively, if you’ve used a button symbol instance or a movie clip symbol instance, add an onRelease( ) event handler method.

Discussion

For Flash to be responsive to the user’s mouse clicks, you’re going to need to write some ActionScript. The ActionScript you’ll need to write is not particularly long, however. The details of the ActionScript you’ll write depend, in part, on which type of button you’ve used.

If you’ve used a Button component:

  1. Create a listener object.

  2. Assign a click( ) method to the listener object.

  3. Register the listener object with the button instance.

A listener object is a code-based construct that basically sits around waiting (listening) for a component to do something. This something for which the listener is waiting is called an event. If the component performs the particular type of event for which the listener is configured, then the corresponding code on the listener object runs. Each type of component can dispatch different types of events. The button component dispatches an event called click when the user clicks it. The way that the listener object knows what code to run at that point is by looking for a method (a group of actions) called click( ).

That may all sound a bit confusing, but a simple example will illustrate ...

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.