PDA

View Full Version : removeChild and clean array?


Dennis_gull
05-31-2008, 12:24 PM
I know I've posted a lot the last days but i almost done with my application and I think this is one of the last problems.

I store dynamically added movieClips inside an array so I later can control them like this:
for each(var m:MovieClip in dlistObject) {
// Do something with m
}

But now I want to be able to remove all the MovieClips but It doesnt work, I've tried to do the classic DOM example:
while(myInstance.firstChild) {
myInstance.removeChild(myInstance.firstChild);
}

but firstChild doesn't work with movieClips (at least not for me).

and I've also tried:
for each(var m:MovieClip in dlistObject) {
myInstance.removeChild(m);
}

but that will give me an error.

So how should I go on now? :confused:


PS:

Im also looking for a way to remove all values inside an Array if thats possible?

norz
05-31-2008, 02:03 PM
myInstance.removeChildAt(0);

lordofduct
05-31-2008, 02:40 PM
The child at the bottom of the display list of a DisplayObjectContainer is 0, at the top is numChildren - 1.

As for completely cleaning out an array. One choice is to just declare a new array.


var arr:Array = new Array();
...//fill arr
arr = new Array(); // array is empty by assumption... thing is the old array actually still exists just with no reference to it, it will be cleaned up by garbage collection. Any contents will be disposed of if no reference to them exists.


what I do though instead of creating a whole new array is just empty it with a simple loop.


var arr:Array = new Array();
...//fill array

while(arr.length) arr.shift();

//you can use pop(), splice(0,1) and many other methods as well... shift takes the object out of the '0' slot. pop takes it out of the final slot (length - 1). splice removes a specific index at a specific length.

//you could also just say: arr.splice(0,arr.length); with out the while loop as well.


choice one instantiates a new array which takes up a tiny bit of memory, but is much faster then option 2. Personally I go with option 2 though and take the speed hit because my array usually is never in excess of 30 or so objects. This and I can better control the destruction of the objects with in.

For instance, say you wanted to clear an array and push them into another array for recyclables. An array of references to movieclips you might use in the recent future to keep from having to instantiate new movieclips. Also say during the emptying you also want to run some closing method on the objects as you dispose of their "literal container" (as opposed to their display container which they might not have). Something could be like playing a closing animation and then removing them from a displayObjectContainer.

Dennis_gull
05-31-2008, 04:55 PM
Thanks for the advice, I had to use removeChildAt(1) or else the parent gets deleted.

About the Array:
Is it possible to see which one is more resource hogging, creating a new array or using the "while(arr.length) arr.switch()" method?