Okay, you're getting a little mistaken advice here.
Quote:
|
samuraiArray.removeChildAt(f)
|
1) removeChildAt removes children from Sprites and Movieclips, not from Arrays. You are correct to use splice to remove an entry from an array
Quote:
Originally Posted by UnknownJoe796
Your code does not work because you are removing them from the array WHILE the loop is running. That won't work.
|
2) No, it's okay because you're looping through the array from highest numbers to lowest.
Quote:
for each(var enemy in enemyArray)
{
removeChild(enemy);
}
enemyArray=new Array();
|
3) This would be another way to remove all the enemies, which is what your code is currently doing, but you said you want your code to only remove certain enemies, not all of them.
To that end, yes, you should have an if statement inside of your for loops to test if it's one that got hit. Sometimes you can use for this hitTestObject or hitTestPoint. You can also figure out the width and height of the two objects and compare them, like "if (player.x+20>enemy.x)&&player.x-20<enemy.x)&&..."
If you've already done the hitTest and the object that got hit is calling this function as a result of that you could have it pass itself as a parameter and use that to find itself in the array:
ActionScript Code:
function removeEnemy(enemy:Sprite):void {
...
...for loop
......if (enemy==array[f])
.........
As for your other question, yeah, a timer might be the way to go. You could set a variable for each, like farmerSpawned=true, and then start a timer that turns it back to false. Another way would be to hitTest the starting point to see if anything is already there.