ForumsProgramming ForumCollision detection in FlashDevelop

1 5456
Mayhem_Dude
offline
Mayhem_Dude
2 posts
Nomad

Hi! I would like to know how do I do collision detection in FlashDevelop (in ActionScript 3) ?

  • 1 Reply
joeybetz
offline
joeybetz
107 posts
Shepherd

Every DisplayObject (MovieClip, Sprite, etc) has hitTestObject() and hitTestPoint() as a method/function (Actionscript 3 only). I would start with these, as they are the easiest to use.

The difference between them is:

hitTestObject - Uses each objects bounding box to test for collision. This is pretty fast, but inaccurate for complex shapes. Perfect for squares or rectangles that don't rotate. (Rotating makes the bounding box bigger as it rotates diagonally).

hitTestPoint - Uses either the objects bounding box or the shape of the object within it. The last Boolean flag tells it whether to use a shapeFlag or not. A value of false says to use the objects bounding box, which is as accurate as hitTestObject, and a value of true tests if the point lies within the graphical drawing of the object (The shape of the object). Setting this to true means the detection will be very accurate to your object. This is kind of slow, so it use it in moderation.

Both of these function return true if there is a collision between the passed values and the object calling it.

Example:

if( myMovie.hitTestObject( hisMovie ) )
//Do Awesome Code

if( myMovie.hitTestPoint( posX, posY, true ) )
//More Awesome Code

I hope this helps. The more complex collision detection code uses quite a bit of math, which I'd be happy to help with as well if need be. This is probably the simplest of what most Flash Games use and very cool tool for Adobe to incorporate.

Showing 1-1 of 1