Note that there are some explanatory texts on larger screens.

plurals
  1. POAS3 - Controlling enemy behaviour for different enemy types
    primarykey
    data
    text
    <p>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:</p> <p>enemyType1 is the enemy movieclip.</p> <pre><code>var enemyType1:EnemyType1 = new EnemyType1(0, 0); var enemies:Array = new Array(); </code></pre> <p>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. </p> <pre><code>if(Math.random() &lt; 0.5) { var newEnemyType1 = new EnemyType1(0, 0); enemies.push(newEnemyType1); addChild(newEnemyType1); } for (var i:int = 0; i &lt; enemies.length; i++) { //Movement } </code></pre> <p>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:</p> <pre><code>var enemyType1:EnemyType1 = new EnemyType1(0, 0); var enemyType2:EnemyType2 = new EnemyType2(0, 0); var enemies:Array = new Array(); </code></pre> <p>And in the game loop:</p> <pre><code>if(Math.random() &lt; 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 &lt; enemies.length; i++) { if(enemies[i] == EnemyType1) { //EnemyType1 Movement } else if(enemies[i] == EnemyType2) { //EnemyType2 Movement } } </code></pre> <p>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.</p> <p>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?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload