views:

44

answers:

3

i don't understand why none of my display objects are not being removed. when i press the button, i'm expecting a trace and removal of both shapes and the button, but nothing happens:

import fl.controls.Button;

var shape1:Shape = new Shape();
shape1.name = "Shape1";
shape1.graphics.lineStyle(4, 0x000000);
shape1.graphics.beginFill(0x000055, 0.5);
shape1.graphics.drawRoundRect(50, 50, 100, 75, 20, 30);
shape1.graphics.endFill();
addChild(shape1);

var shape2:Shape = new Shape();
shape2.name = "Shape2";
shape2.graphics.lineStyle(4, 0xFFFF99);
shape2.graphics.beginFill(0x550000, 0.5);
shape2.graphics.drawRoundRect(100, 75, 200, 175, 50, 10);
shape2.graphics.endFill();
addChild(shape2);

button1.addEventListener(MouseEvent.CLICK, pushButton);
function pushButton(evt:MouseEvent):void
    {
    for(var amount:int = numChildren; amount == 0; amount--)
        {
        trace(amount);
        var disObj:DisplayObject = getChildAt(amount);
        trace("Removing " + disObj.name);
        removeChildAt(amount);
        }
    }

i realize there are better ways of accomplishing this, but i'm learning and therefore only interested in why the above code doesn't work.

+2  A: 

Change your for loop to: for(var amount:int = numChildren - 1; amount >= 0; amount--)

Wesley Petrowski
+2  A: 

That's not a very reliable way to remove child objects from a DisplayObject, a while loop like this will sort it out though:

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

With your for loop implementation you'll find that some objects will be removed, but it won't take them all out. The while loop will keep going until there's nothing left. Just replace 'displayObject' with whatever container object you want to remove stuff from.

debu
thanks for this.
TheDarkInI1978
+2  A: 

It looks like your loop continuance condition will never run, because you're stating that the loop will only 'continue' when the children == 0

Jason M