Creating interactivity and visual effects 577
To detect a collision between a movie clip and the mouse pointer:
1. Select the first frame on Layer 1 in the Timeline.
2. Select Window > Actions to open the Actions panel, if it is not already open.
3. Add the following code in the Actions panel:
this.createEmptyMovieClip("box_mc", 10);
with (box_mc) {
beginFill(0xFF0000, 100);
moveTo(100, 100);
lineTo(200, 100);
lineTo(200, 200);
lineTo(100, 200);
lineTo(100, 100);
endFill();
}
this.createTextField("status_txt", 999, 0, 0, 100, 22);
var mouseListener:Object = new Object();
mouseListener.onMouseMove = function():Void {
status_txt.text = _level0.hitTest(_xmouse, _ymouse, true);
}
Mouse.addListener(mouseListener);
4.
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 Frame 1 on the Timeline.
3. Select Window > 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();
};