Question

What I am trying to do is check for collision of a movie clip instance named enemy with an instance of a movie clip called laser (which is inside a movie clip called ship).

When enemy collides with ship I want the main timeline to go to frame 2.

When enemy collides with laser I want the main timeline to go to frame 3.

The problem is that when ship.laser collides with enemy, it also counts as if ship was colliding with enemy.

Is there a way to check for the collision of ship.laser and enemy without checking for ship and enemy at the same time?

addEventListener(Event.ENTER_FRAME, collide);
function collide(event:Event)
{
    if (enemy.hitTestObject(ship) )
    {
        gotoAndStop(2);
        trace("hit Ship");

    }

    if (enemy.hitTestObject(ship.laser))
    {
        trace("hit laser");
        gotoAndStop(3);

    }
}
Was it helpful?

Solution

It's normal that if the enemy collides with laser it also collides with ship, because laser is part of the ship.

What you can do is just to check if enemy collided with laser:

if (enemy.hitTestObject(ship) )
{
    if (enemy.hitTestObject(ship.laser))
    {
        // collides with ship AND laser
    } else {
        // collides with ship only, but not with the laser
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top