Welcome To VertexTemplates.com Flash MX 2004 Tutorials Area - Collision Detection |
![]() |
|||
Learn how to use hitTarget to detect collisions and how to make it work for your movies.
stop();
4. Now we can get into programming the collision detection. We are going to look for the collision detection on every frame of our movie. If there is a collision the box will then change colors and our result text box will display a message telling the usre that there has been a collision. If there is no collision the color will stay the same and our result text box will display that there is no collision. The first bit of code we are going to add is going to make our smaller box draggable. Make a new layer on your root timeline and name it actions. Add the following code on the first frame.
_root.box_mc.onPress = function() {
this.startDrag(false, 0, 0, 400, 200);
this.onMouseMove = function() {
updateAfterEvent();
}
}
_root.box_mc.onRelease = function() {
stopDrag();
};
Hopefully all of this looks kind of familiar, but if it doesn't what we are doing here is using our movie clip as a button and telling it that whenever it is pressed it should be dragged by the mouse and when it is released it will stop being dragged. In the startDrag() function we set "lock center" to false and the numbers are simply the boundaries of where we can drag our box (left, top, right, bottom respectively). Something that might be new is our onMouseMove() function. What we are telling flash to do is to update the box position everytime the mouse moves. Without this bit of code the box will move the mouse position everyframe, but our mouse is moving around alot and it will look pretty choppy without this little addition.
5. The last bit of code we need to add is our collision detection function. To do this we are going to use flash's built in hitTarget() function. Add the following code under what we just wrote.
_root.box_mc.onEnterFrame = function() {
if(this.hitTest(_root.target_mc)) {
this.gotoAndStop(2);
_root.result="the boxes are touching";
} else {
this.gotoAndStop(1);
_root.result="the boxes are not touching";
}
};
In order to test every frame if there has been a collision detection we use an onEnterFrame clip event. Then we test to see if your box movie clip by using hitTest. Then we tell the movie clip to change colors and change our dynamic to display text telling the user that there has been a collision. Then if there is not a collision we make sure that our movie clip goes back to it's original color and reset our text message.
6. Where to go from here. This function has many uses, but also many limitations. One limitation is that the hitTest will not work accurately with odd shapes, because flash tests to see if any part of the movie clips bounding box touches another movie clips bounding box touches. So cirlces, stars, and other shapes don't function properly. However this function has many uses. I am currently working on a Flash VCR where the user drags tapes to a vcr and I use hitTest to see if the tape is on the VCR input. I hope this tutorial has helped you out, if you have any questions be sure to email me. |
||||
| Home Web Templates Affiliate Program Template Tour Custom Website Design Tutorials Articles Contact Us FAQ's All Rights Reserved © 2005 VertexTemplates.com Read our Terms Of Use and Privacy Policy Flash Templates by Metamorphosis Design |
||||