Flash Actionscript + detecting collision at edges
-
11-10-2019 - |
Question
I have two circular objects. I'm trying to detect as soon as the circles touch. The trace detects a collision when one circle reaches the center of the other but I want the collision to be detected as soon as the circles touch.
My two symbols are coin_mc and mugbounds_mc.
function checkHitArea(evt:Event)
{
if (coin_mc.hitTestPoint(mugbounds_mc.x,mugbounds_mc.y, true)) {
coin_mc.x=-1;
coin_mc.y=-1;
trace("Hit Mug"); // Is triggered when coin_mc reaches center of mugbounds_mc
}
else
{
trace("Didn't Hit Mug");
}
}
Solution
Try this:
addEventListener(Event.ENTER_FRAME, checkHitArea)
function checkHitArea(e:Event)
{
a.x += 2;
if (a.hitTestPoint(b.x,b.y, false))
{
// do our in-circle check
if((a.x - b.x) * 2 + (a.y - b.y) * 2 <= (a.width/2 + b.width/2) * 2)
{
trace("hit");
}
}
else
{
trace("Didn't Hit Mug");
}
}
I renamed your movie clips to a and b.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow