User Guide
192 Chapter 6: Creating Interaction with ActionScript
Detecting collisions
The
hitTest() method of the MovieClip class detects collisions in a SWF file. It checks to see if
an object has collided with a movie clip and returns a Boolean value (
true or false).
There are two cases in which you would want to know whether a collision has occurred: to test if
the user has arrived at a certain static area on the Stage or to determine when one movie clip has
reached another. With
hitTest(), you can determine these results.
You can use the parameters of
hitTest() to specify the x and y coordinates of a hit area on the
Stage or use the target path of another movie clip as a hit area. When specifying
x and y,
hitTest() returns true if the point identified by (x, y) is a non-transparent point. When a target
is passed to
hitTest(), the bounding boxes of the two movie clips are compared. If they overlap,
hitTest() returns true. If the two boxes do not intersect, hitTest() returns false.
“True” appears in the text field whenever the pointer is over the car body.
You can also use
hitTest() to test a collision between two movie clips.
“True” appears in the text field whenever one movie clip touches the other.
The following procedures show how to detect collision using the car example.