0

So here im checking for when a bullet hits an enemy ship in my game. Im trying to check the type of enemy in the array by object name to do specific things for that enemy, code is below.

for (var i = bullets.length - 1; i >= 0; i--) {
        for (var j = enemies.length - 1; j >= 0; j--) {
            if (_bullets[i].hitTestObject(enemies[j])) {

                if (enemies[j] == EnemyYellow)  {
                        trace("do something");
                    }

                stage.removeChild(enemies[j]);
                stage.removeChild(bullets[i]);
                bullets.splice(i, 1);
                enemies.splice(j, 1);
                return;
            }
        }
    }

This is something like I thought would work, but I would appreciate if anyone could help me out as im not sure how to do it.

    if (enemies[j] == EnemyYellow) {
           trace("do something");
        }

2 Answers 2

2

You can use the keyword is

if (enemies[j] is EnemyYellow) {
     trace("do something");
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can also add a method getType to the Enemy class. This solution is not better for this particular case but may be useful in some other cases. For example, you can have enemies of the same class but returning different types.

if (enemies[j].getType() == EnemyType.ENEMY_YELLOW) // do something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.