Reputation: 1
How to remove a child in an array in ActionScript 3.0?
Here's my code. I put the child I want to remove in an array called nailList[]
and put it on gameStage.
function makeNails():void
{
for (var i=0; i< 3; i++)
{
for (var j=0; j<3; j++)
{
var tempNail = new Nail2(j * C.NAIL_DISTANCE + C.NAIL_START_X,
i * C.NAIL_DISTANCE + C.NAIL_START_Y);
nailList.push(tempNail);
gameStage.addChild(tempNail);
}
}
gameStage.addEventListener(Event.ENTER_FRAME,update);
gameStage.addEventListener(MouseEvent.CLICK, clickToHit);
}
Here's the code I used to remove the nails. But nothing happens after this code. The nails are still there.
for (var j:int = nailList.length; j >= 0; j--)
{
removeNails(j);
trace("Remove Nail");
}
function removeNails(idx:int)
{
gameStage.removeChild(nailList[idx]);
nailList.splice(idx,0);
}
I want to remove the MovieClip
so that I can restart the game and add new ones.
Upvotes: 0
Views: 185
Reputation: 46027
Valid array index range is from 0
to length - 1
. You are trying to call removeChild
with nailList[nailList.length]
which is invalid and removeChild
should fire an error. You need to change the initial value of j
to nailList.length - 1
.
for (var j:int = nailList.length - 1; j >= 0; j--)
^^^
Another problem is (as pointed in the comment) the second parameter of splice
is delete count. So you need to use 1 instead of 0.
nailList.splice(idx, 1);
^
Upvotes: 1
Reputation: 699
You can use splice
to remove the child of an array.
arrName.splice(2,1);
Hope it will help.
Upvotes: 0