PDA

View Full Version : Removing composite children of UIComponent


ksr732
03-13-2009, 08:14 PM
Hi,
I'm drawing a dynamic Sprite Object which has labels and corresponding tooltips. When I use the following piece of code to delete all the children of UIComponent (c) , only some labels are getting removed while the others remain.

var i:uint=0;
while(i< c.numChildren)
{
c.removeChildAt(i);
i++;
}

i've also tried :
for (var i:uint=0; i<c.numChildren; i++)
c.removeChildAt(0);

Kindly help me solve this problem
Thanx :)

Sly_cardinal
03-14-2009, 02:26 PM
You're affecting the number of children as you remove them so you are unable to remove them properly if you count forward.

Instead remove them in reverse order:


for (var i:int = c.numChildren - 1; i >= 0; i--)
{
c.removeChildAt(i);
}


or alternatively, keep removing the first child until there are none left:


while (c.numChildren > 0)
{
c.removeChildAt(0);
}