PDA

View Full Version : How to select all the children of a DisplayObjectContainer



benkyoto
October 2nd, 2007, 02:45 AM
I generated an XML gallery inside a DisplayObjectContainer (the square) placed on the stage. Since all the thumbnails have listeners applied to them, I thought it was a good idea to remove all listeners and child objects when closing the square container. There are many gallery categories which could be selected afterwards and every one has the same functionality so I'm concerned about performance dropping from hundreds of active listeners left behind. To do so, I worte this code

(metiches() is another custom check function unrelated to this process)


function closegal(evt:MouseEvent):void
{
var thesquare = evt.target.parent;
trace("the square "+thesquare.name+" is gonna die!");
korose(thesquare);
metiches();
}

function korose(die:DisplayObjectContainer)
{
var morira:DisplayObjectContainer=die;
var i:int=new int;

for (i=0; i<morira.numChildren; i++)
{
var thisMC:Object =morira.getChildAt(i);
thisMC.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
thisMC.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
thisMC.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
morira.removeChildAt(i);

}
removeChild(morira);
trace(morira.willTrigger(MouseEvent.MOUSE_DOWN));
}

I commented the line second from last (marked in red), so I could visually confirm that all the thumbnails and other elements inside the square container were gone, but when I tested the thing, all the odd numbered thumbnails were left behind. The trace returns "false", so I guess the EventListener's were removed.
Does anyone have a clue on why half of the thumbnail gallery children were not removed. Also, is the last line enough to make sure there are no more active listeners related to this container?

As always I appreciate any help, insights...

SlowRoasted
October 4th, 2007, 12:14 PM
I would also like the answer to this. I'm working on the same issue.

natebishop
October 26th, 2007, 05:58 PM
This was killing me for a little while, too. The index position of all the other children (above the child being removed) are decreased by 1. So removeChildAt(0) instead of your variable.

senocular
October 26th, 2007, 06:51 PM
the best way is to walk backwards
var i = morira.numChildren
while(i--) {
morira.removeChildAt(i);
}

Even if you don't delete them all, order will be maintained because all shifting occurs with the display objects you've already cycled through (those greater than i)