0

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.

0

2 Answers 2

1

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);
                     ^
Sign up to request clarification or add additional context in comments.

2 Comments

There is another smaller mistake in the code, splicing should be done for 1 object instead of 0 in his code.
Thank you so much i fixed it already and this did the trick. Im quite new in actionscript so i cant get a hold of some of its logic. Thanks again.
0

You can use splice to remove the child of an array.

arrName.splice(2,1);

Hope it will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.