EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 106

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
106 Chapter 5: Creating Interaction with ActionScript
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:
1 Create a new movie clip on the Stage, and enter box as the instance name in the
Property inspector.
2 Create a dynamic text box on the Stage, and enter status as the instance name in the
Property inspector.
3 Select the first frame on Layer 1 in the Timeline.
4 Select Window > Development Panels > Actions to open the Actions panel, if it is not
already visible.
5 Add the following code in the Actions panel:
box.onEnterFrame = function () {
status.text = this.hitTest(_xmouse, _ymouse, true);
}
6 Select Control > Test Movie, and move the mouse over the movie clip to test the collision.
The value
true is displayed whenever the mouse is over a nontransparent pixel.
To perform collision detection on two movie clips:
1 Drag two movie clips to the Stage, and give them the instance names car and area.
2 Create a dynamic text box on the Stage, and enter status as the instance name in the
Property inspector.
3 Select the first frame on Layer 1 in the Timeline.
4 Select Window > Development Panels > Actions to open the Actions panel, if it is not
already visible.
5 Enter the following code in the Actions panel:
area.onEnterFrame = function () {
status.text=this.hitTest(car);
}
car.onPress = function (){
this.startDrag(false);
updateAfterEvent();
}
car.onRelease = function () {
this.stopDrag();
}
6 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, see MovieClip.hitTest() in Chapter 12, ā€œActionScript Dictionary,ā€
on page 205.

Table of Contents

Related product manuals