tailedmouse
tailedmouse

Reputation: 365

simple array help in actionscript 3

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);
}

Upvotes: 0

Views: 48

Answers (2)

bitmapdata.com
bitmapdata.com

Reputation: 9600

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);
}

Upvotes: 0

Alexis King
Alexis King

Reputation: 43902

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);
}

Upvotes: 1

Related Questions