Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have created multiple movieclips with animations within them. It is an obstacle avoidance game and I cannot seem to be able to get my enemies to contact my playableCharacter. The enemies I have created are each embedded on certain levels of my game. I have created an array, enemiesArray to have each of my enemies placed within it. Here is the code for that:

//step 1: make sure array exists
if(enemiesArray!=null && enemiesArray.length!=0)
    //step 2: check all enemies against villain
    for(var i:int = 0;i < enemiesArray.length; i++)
        //step 3: check for collision
            //step 4: do stuff

            villain = null;

What I am unsure of is whether or not my enemiesArray is actually holding the movieclips I have suggested. If it was, this code would be tracing back a "HIT" for every time I ran into an enemy and would kill my character. It is not doing that however.

I am thinking I have to push my movieclips into my array but I don't know how to do that or where for that matter.

Any and all help would be much appreciated.

share|improve this question
To the point questions are best. The answer you seek is to use a debugger. Step through your code and see what the values are at run time. Typically these, "Here's my code, what's my problem?" questions are not good questions. You can clear up a lot of your own questions with a little debugger work. And problems like this, where you're not even sure what's in your array, are ideal problems for the debugger. Debugger, debugger debugger. Have I said that enough? Good luck! – Byte56 Jul 16 '12 at 23:43
Also you shouldn't iterate your array like that and use splice inside this loop... either iterate backwards (from enemiesArray.length - 1 to 0) or push the enemies that should be removed to a delete-list and remove them later on. If you want to end the game as soon as a hit occurs, then above code is fine, but you should exit the loop with a break statement. – bummzack Jul 17 '12 at 8:31
The enemiesArray.length check is redundant. A forloop automatically skips if the condition is not met. 0 is not smaller than 0. Therefor the for loop is never processed. As to some addition to @bummzack use a while loop instead if you are going to decrease the variable iterator (i). Well at least it reads a bit cleaner since you control 'i' and not the loop. – Sidar Aug 16 '12 at 6:59

If you add the enemies to the stage dynamically, via code, you should add a line of code which pushes the enemy onto the enemiesArray every time you create a new enemy and add it to the stage.

For example,

var myEnemy:Enemy = new Enemy();
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.