Removing movieclips of an array from the stage
I need to remove every object that is part of an a开发者_运维问答rray from the stage. What I have right now only removes one of the objects.
this.removeChild(enemyList.splice(0)[0]);
I've tried several variations of this with either the same result or an error. Is there another way to do this or am I just doing it wrong?
Also, what is the zero in hard brackets for? I haven't seen that in many tutorials/explanations, but when I take it out, I get all kinds of errors.
What you are doing is just removing the first object in the array. The line
this.removeChild(enemyList.splice(0)[0]);
is basically removing the first child from the array ([0]
), and splicing it out of the array at the same time (enemyList.splice(0)
). To remove them all you need a loop
There are a few options you can do:
Slow: Use a while
loop to go through the list as long as there's any objects in there. Remove each one and splice the list to remove it from the list as well (0 is the position and 1 means we only want to splice one object from it)
while( enemyList.length > 0 )
{
this.removeChild( enemyList[0] );
enemyList.splice( 0, 1 );
}
Bit faster: Go through the list while there's still objects, and remove the last one. Pop()
it from the array while you're there
while( enemyList.length > 0 )
{
this.removeChild( enemyList[enemyList.length - 1] );
enemyList.pop();
}
Possibly a bit faster than the second one (not tested), but faster than the first: Go through the array in reverse order and remove each child, splicing as you go along
var len:int = enemyList.length
for( var i:int = len - 1; i >= 0; i-- )
{
this.removeChild( enemyList[i] );
enemyList.splice( i, 1 );
}
Fastest: Just go through the array and remove each child. At the end of the loop, clear the array by setting it's length to 0 (this removes the references and lets it be garbage collected)
var len:int = enemyList.length;
for( var i:int = 0; i < len; i++ )
this.removeChild( enemyList[i] );
// clear the array
enemyList.length = 0;
Some points: Splicing is slower than using pop()
and after you splice the object out, the runtime needs to move all of the later objects one space down (e.g. if you splice enemyList[1], then enemyList[2] needs to move to position 1 to make up the gap). If you're going to splice, always splice from the back (as it's quicker: there's no objects to move up). For this same reason, pop()
is faster than slice()
(which removes the first object in the array)
Never ever modify the array (e.g. splice()
) if you're looping through it forwards, as you're modifying the length of it while looping. For example, if your array was var a:Array = ["obj1", "obj2", "obj3"]
and you spliced it by doing this:
for( var i:int = 0; i < a.length; i++ )
a.splice( i, 1 );
Then in the first pass, you would remove the object at position 0 (in this case "obj1"). "obj2" and "obj3" now move to position 0, and 1 respectively. In the next loop, i
would be 1 (in this case pointing to "obj3") and that would be removed. The loop would now stop, even though you've still got an object left in it.
If you're going to modify an array by removing an object during the loop, then always loop backwards
What you are doing is deleting an index in the array, by splicing it. And you're also using Array.splice
incorrectly.
What you want is something that goes through every enemy in enemyList
, and remove each one.
for (var i:int = 0; i < enemyList.length; i++)
{
this.removeChild(enemyList[i]);
}
精彩评论