The hitTest
method of the MovieClip object detects collisions in a movie. 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, and to determine when one movie clip has reached another. With the hitTest
method, you can determine these results.
You can use the parameters of the hitTest
method 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 nontransparent 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
.
Move the mouse pointer over the shape in the movie to test the collision. The results of the hitTest
method are returned in the text field.
You can also use the hitTest
method to test a collision between two movie clips.
To test the collision, drag the car movie clip so that it touches the parking area movie clip. The results of the hitTest
method are returned in the text field.
The following procedures show how to detect collision using the car example.
To perform collision detection between a movie clip and a point on the Stage:
status
as the instance name in the Property inspector.set variable
, and name the variable _root.status
. Then select the Expression check box for Value and enter the following code in the Value text box:hitTest(_root._xmouse, _root._ymouse, true)
Flash automatically adds the onClipEvent
handler.
onClipEvent
action and select enterFrame
as the event.The value true
is displayed whenever the mouse is over a nontransparent pixel.
To perform collision detection on two movie clips:
car
and area
.status
as the instance name in the Property inspector.area
and choose Window > Actions if the Actions panel is not already visible.hitTest
test, in the Actions toolbox, click the Actions category, click Miscellaneous Actions, and double-click evaluate
. Enter the following code in the Expression text box:
_root.status=this.hitTest(_root.car
);
Flash automatically adds the onClipEvent
handler.
onClipEvent
action and select enterFrame
as the event.car
from the jump menu at the top of the Actions panel.startDrag
.Flash automatically adds the onClipEvent
handler.
onClipEvent
action in the Script pane and choose the Mouse down
event. Your code should look like this:
onClipEvent (mouseDown) { startDrag("", true, 4, 70, 396, 273); }
stopDrag
.Flash automatically adds the onClipEvent
handler.
onClipEvent
action in the Script pane and choose the Mouse up
event. Your code should look like the following code:
onClipEvent (mouseDown) { startDrag("", true, 4, 70, 396, 273); } onClipEvent (mouseUp) { stopDrag(); }
Whenever the bounding box of the car intersects the bounding box of the area, the status is true
.
For more information, see MovieClip.hitTest()
.