7.6. Defining Hit Areas for Movie Clips

Problem

You want to define a hit area for a movie clip used as a button.

Solution

Create a second movie clip to serve as the hit area. Then assign a reference to the hit area movie clip to the hitArea property of the button movie clip.

Discussion

When you create button symbols at authoring time, you can create alternate hit areas using the Hit frame of the button’s timeline. This can be very useful if the artwork in the button is not solid or if you want to define a hit area that is otherwise different from the shape defined by the button’s artwork.

You can also define hit areas for movie clips used as buttons. All movie clips have a built-in property named hitArea. If hitArea is left undefined, Flash automatically uses the movie clip’s artwork as the hit area. However, you can assign the hitArea property a reference to another movie clip to serve as the hit area.

The following example shows how a custom hit area can be useful when dealing with odd shapes or outlines. Using code, we generate a movie clip that contains 100 small, unfilled circles that are aligned to form a square grid. We then assign an onRelease( ) event handler method to the movie clip instance. Testing the code reveals that the movie clip’s rollover state is activated only when the mouse pointer is directly over one of the circle outlines.

// Include the DrawingMethods.as file from Chapter 4 for its drawCircle(  ) method. #include "DrawingMethods.as" // Create the button movie ...

Get Actionscript 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.