I've been working on a simple game in AS3, but I can't seem to add in movement for the different types of enemies. So far it only works for 1 enemy type:
enemyType1 is the enemy movieclip.
var enemyType1:EnemyType1 = new EnemyType1(0, 0);
var enemies:Array = new Array();
And in my game loop, I randomly spawn the enemies and have a for loop, which loops through all the enemies, and performs the movement for each enemy.
if(Math.random() < 0.5)
{
var newEnemyType1 = new EnemyType1(0, 0);
enemies.push(newEnemyType1);
addChild(newEnemyType1);
}
for (var i:int = 0; i < enemies.length; i++)
{
//Movement
}
The problem comes when creating different types of enemies and determining which enemy type the enemy is, so that the correct type of movement will be used. This becomes:
var enemyType1:EnemyType1 = new EnemyType1(0, 0);
var enemyType2:EnemyType2 = new EnemyType2(0, 0);
var enemies:Array = new Array();
And in the game loop:
if(Math.random() < 0.5)
{
var newEnemyType1 = new EnemyType1(0, 0);
enemies.push(newEnemyType1);
addChild(newEnemyType1);
var newEnemyType2 = new EnemyType2(0, 0);
enemies.push(newEnemyType2);
addChild(newEnemyType2);
}
for (var i:int = 0; i < enemies.length; i++)
{
if(enemies[i] == EnemyType1)
{
//EnemyType1 Movement
}
else if(enemies[i] == EnemyType2)
{
//EnemyType2 Movement
}
}
Except it doesn't work. I can't recognize enemies[i] for being an object. I have also looked at multidimensional arrays for each enemy type, but couldn't get it to work.
So the question is, how can I detect for a specific object in an array? Or is this completely the wrong way to go about controlling multiple enemy types?
Thanks.