luke  db
luke db

Reputation: 25

action script 3 - issue with arrays

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");
        }

Upvotes: 0

Views: 39

Answers (2)

Andrei Nikolaenko
Andrei Nikolaenko

Reputation: 1074

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

Upvotes: 1

Pan
Pan

Reputation: 2101

You can use the keyword is

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

Upvotes: 2

Related Questions