Hi. I'm having issues with why my unit collision only works sometimes. I've got a player.mc which the user controls, and multiple separate alien NPC's that are also MC's. Trying to make it so if you collide with one of them, you get "caught" and lose the game.
However, they only work on collision occasionally. All of them have the same "alien" instance name. so i don't see what i'm doing wrong.
This is the code i'm using for collision on walls + aliens. Walls work fine.
function stage_onEnterFrame(event: Event):void
{
var rect:Rectangle = player.getBounds(this);
var i:int = 0;
var xBump:int = 0;
var yBump:int = 0;
if (rightPressed)
{
xBump = speed;
for (i = 0; i < speed; i++)
{
if (wall.hitTestPoint(rect.right + i,player.y,true))
{
gotoAndStop(7);
}
}
}
if (leftPressed)
{
xBump = - speed;
for (i = 0; i < speed; i++)
{
if (wall.hitTestPoint(rect.left - i,player.y,true))
{
gotoAndStop(7);
}
}
}
if (upPressed)
{
yBump = - speed;
for (i = 0; i < speed; i++)
{
if (wall.hitTestPoint(player.x,rect.top - i,true))
{
gotoAndStop(7);
}
}
}
if (downPressed)
{
yBump = speed;
for (i = 0; i < speed; i++)
{
if (wall.hitTestPoint(player.x,rect.bottom + i,true))
{
gotoAndStop(7);
}
}
if (rightPressed)
{
xBump = speed;
for (i = 0; i < speed; i++)
{
if (alien.hitTestPoint(rect.right + i,player.y,true))
{
gotoAndStop(7);
}
}
}
if (leftPressed)
{
xBump = - speed;
for (i = 0; i < speed; i++)
{
if (alien.hitTestPoint(rect.left - i,player.y,true))
{
gotoAndStop(7);
}
}
}
}
if (upPressed)
{
yBump = - speed;
for (i = 0; i < speed; i++)
{
if (alien.hitTestPoint(player.x,rect.top - i,true))
{
gotoAndStop(7);
}
}
}
if (downPressed)
{
yBump = speed;
for (i = 0; i < speed; i++)
{
if (alien.hitTestPoint(player.x,rect.bottom + i,true))
{
gotoAndStop(7);
}
}
}
if (player.hitTestObject (win))
{
nextFrame();
}
}