0

I am doing a program where I am creating instances by arrays but I am not sure on how to get rid of them later.. to explain more clearly here is my code:

(I quickly whipped up an example...so that's why there is only one variable in the code)

for (var q:int = 0; q < caw1.length; q++)
{
    addChild(caw1[q]);
    caw1[q].x = 9;
    caw1[q].y = 833;
}



half2.addEventListener(MouseEvent.CLICK, nxt2);
function nxt2(e:MouseEvent)
{
            removeChild(half2);
    removeChild(caw1[1]);
    half2.removeEventListener(MouseEvent.CLICK, nxt2);
}
2
  • What do you mean "get rid of them later"? When do you want to get rid of them, and how? Do you want to remove them from the array, from the stage, or both? Commented Feb 13, 2013 at 6:26
  • I want to get rid of them after that half 2 event ends . just from the stage. Commented Feb 13, 2013 at 6:27

2 Answers 2

1

What's wrong with just looping through them again?

function nxt2(e:MouseEvent)
{
    removeChild(half2);
    for (var i:int = 0; i < caw1.length; i++) {
        removeChild(caw[i]);
    }
    half2.removeEventListener(MouseEvent.CLICK, nxt2);
}
Sign up to request clarification or add additional context in comments.

1 Comment

@tailedmouse Haha, no problem, everyone starts somewhere. ;)
0

FP 11 later , A new method has been added. that is removeChildren(). using a removeChildren() rather than using for loop with removeChild(). is more reasonable.

Removes all child DisplayObject instances from the child list of the DisplayObjectContainer instance. The parent property of the removed children is set to null , and the objects are garbage collected if no other references to the children exist.

The garbage collector reallocates unused memory space. When a variable or object is no longer actively referenced or stored somewhere, the garbage collector sweeps through and wipes out the memory space it used to occupy if no other references to it exist.

Try this:

function nxt2(e:MouseEvent)
{
    removeChild(half2);
    removeChildren(0,caw1.length-1);
    half2.removeEventListener(MouseEvent.CLICK, nxt2);
}

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.