Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
        if(villain.hitTestObject(enemiesArray[i]))
        {
            //step 4: do stuff
            trace("HIT!");
            removeChild(enemiesArray[i]);
            enemiesArray.splice(i,1);

            removeChild(villain);
            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
2  
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
add comment

1 Answer

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();
stage.addChild(myEnemy);
enemiesList.push(myEnemy);
share|improve this answer
add comment

Your Answer

 
discard

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