2010-03-19 59 views
0

我不明白为什么我的显示对象没有被删除。当我按下按钮,我希望能够获得形状和按键的跟踪和去除,但没有任何反应:Actionscript删除显示对象

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

我知道有实现这一点有更好的方法,但我学习,所以只感兴趣的是为什么上面的代码不起作用。

回答

3

更改您的for循环:for(var amount:int = numChildren - 1; amount >= 0; amount--)

2

这并不是除去孩子一个非常可靠的方法对象从一个DisplayObject,像这样的while循环,虽然整理出来:

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

用你的循环实现你会发现一些对象将被删除,但它不会把它们全部拿出来。 while循环将继续前进,直到没有剩下任何东西。只需将'displayObject'替换为你想从中删除东西的任何容器对象。

+0

感谢。 – TheDarkIn1978 2010-03-19 22:23:18

2

它看起来像你的循环继续条件永远不会跑,因为你指出的循环只会“继续”当孩子== 0