User Guide
Creating interactivity and visual effects 193
To detect a collision between a movie clip and a point on the Stage:
1.
Create a new movie clip on the Stage, and enter box_mc as the instance name in the
Property inspector.
2.
Select the first frame on Layer 1 in the Timeline.
3.
Select Window > Development Panels > Actions to open the Actions panel, if it is not
already open.
4.
Add the following code in the Actions panel:
this.createTextField("status_txt", 999, 0, 0, 100, 22);
status_txt.border = true;
box_mc.onEnterFrame = function() {
status_txt.text = this.hitTest(_xmouse, _ymouse, true);
};
5.
Select Control > Test Movie, and move the pointer over the movie clip to test the collision.
The value
true appears whenever the pointer is over a non-transparent pixel.
To perform collision detection on two movie clips:
1.
Drag two movie clips to the Stage, and give them the instance names car_mc and area_mc.
2.
Select the first frame on Layer 1 in the Timeline.
3.
Select Window > Development Panels > Actions to open the Actions panel, if it is not
already visible.
4.
Enter the following code in the Actions panel:
this.createTextField("status_txt", 999, 10, 10, 100, 22);
area_mc.onEnterFrame = function() {
status_txt.text = this.hitTest(car_mc);
};
car_mc.onPress = function() {
this.startDrag(false);
updateAfterEvent();
};
car_mc.onRelease = function() {
this.stopDrag();
};
5.
Select Control > Test Movie, and drag the movie clip to test the collision detection.
Whenever the bounding box of the car intersects the bounding box of the area, the
status is
true.
For more information about the methods of the Color class, see the Color class entry in
ActionScript Dictionary Help. For more information, see
MovieClip.hitTest() in ActionScript
Dictionary Help.