<classwork>

Collision


The easiest way to do collision detection is to use drag and drop and _droptarget which will detect the movie clip that you are currently hovering over. To aid you in dropping the object on the target.

note: _drop target returns the absolute path in slash syntax notation of the movie clip instance on which my_mc was dropped. The _droptarget property always returns a path that starts with a slash (/). To compare the _droptarget property of an instance to a reference, use the eval() function to convert the returned value from slash syntax to a dot syntax reference.

You MUST perform this conversion if you are using ActionScript 2.0, which does not support slash syntax.

Example

The following example evaluates the _droptarget property of the garbage movie clip instance and uses eval() to convert it from slash syntax to a dot syntax reference. The garbage reference is then compared to the reference to the trash movie clip instance. If the two references are equivalent, the visibility of garbage is set to false. If they are not equivalent, the garbage instance is reset to its original position.

if (eval(garbage._droptarget) == _root.trash) {
garbage._visible = false;
}

download the drop tutorial to learn more (file name: 11drop.fla)
right click on the file (control-click for Mac) and select to download to your drive.


More Advanced Collision Detection with .hitTest

1. Create these functions on stage in Frame 1.

spaceship_mc.onPress = function (){
this.startDrag();
};
spaceship_mc.onRelease = function (){
this.stopDrag();
};
_root.onEnterFrame = function(){
if (spaceship_mc.hitTest(asteroid_mc) == true){
asteroid_mc.nextFrame();
}else{
asteroid_mc.prevFrame();
}
}

2. Create two movie clips, one with an instance name of spaceship_mc, and one with an instance name of asteroid_mc. Give asteroid_mc a two frame cycle with an explosion on the second frame (stop actions on both frames).