2010-03-19 63 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