all groups > flash actionscript > august 2005 >
You're in the

flash actionscript

group:

Detecting Overlap for the Purpose of Detecting Collision


Re: Detecting Overlap for the Purpose of Detecting Collision tralfaz
8/10/2005 2:45:38 PM
flash actionscript:
[quoted text, click to view]

Look up hitTest
tralfaz

Detecting Overlap for the Purpose of Detecting Collision SIMphilip
8/10/2005 9:22:33 PM
Hello everyone,

Is there a way to check to see if two objects are overlapping using
ActionScript? It seems to me that there should be a way to do this, since
buttons have a hit area...shouldn't there be some sort of way to see if two
movie clips' "hit" areas intersect?

In case anyone is wondering, I need to know this because I am designing a game
in Flash, and I need to be able to tell when the player (or an NPC) runs into
an object that it cannot just walk over.

Thanks very much for any help anyone can provide.

--Philip

Re: Detecting Overlap for the Purpose of Detecting Collision SIMphilip
8/11/2005 12:00:00 AM
Hello again,

Thanks, I tried that...however for some reason it is not working properly :(.
I am attaching my code...if you can figure out what I'm doing wrong I'd be
really appreciative.

Btw, I tried changing the movie clip to a button setting the hit area to be
the walls as I had set them to be. Also, note that the walls were a very
irregular shape; could it be that the hit area is considered to be everything
within the boundaries of the shape that I have drawn, and thus that is why it
wasn't working?

Oh, and as for what happened...when I ran it as it is in this code, it
basically always thought that the two objects overlapped, and so the character
couldn't move at all. When I did it with the button instead of the movie clip,
it rarely to never said that they overlapped (even when it was supposed to), so
the character was free to walk about the stage (and through the walls!), which
was not supposed to happen.

Thanks again.

--Philip


function checkCollisions(the_mc:MovieClip,oldX:Number,oldY:Number)
{
for (var name5 in _root)
{
if (typeof this[name5] == 'movieclip' || typeof this[name5] == 'object')
{
//go through the cycle and see if there are any collisions
trace(name5);
if (name5 == "walls" && the_mc.hitTest(this[name5]) == true)
{
the_mc._x = oldX;
the_mc._y = oldY;
trace("wall hit");
}
}
}
}
AddThis Social Bookmark Button