2010-03-19 9 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(var amount:int = numChildren - 1; amount >= 0; amount--) 아이를 제거하는 매우 신뢰할 수있는 방법은의 DisplayObject에서 객체 아니다

2

이 같은 while 루프하지만 그것을 밖으로 정렬합니다 :

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

을 당신을 위해 함께 루프 구현을 사용하면 일부 객체가 제거되지만 완전히 제거되지는 않습니다. while 루프는 아무것도 남지 않을 때까지 계속됩니다. 'displayObject'를 제거하려는 컨테이너 객체로 바꿉니다. 당신은 루프는 '계속'할 것을 주장하고 있기 때문에

+0

감사합니다. – TheDarkIn1978

2

그것은 실행되지 않습니다 루프 연속성 조건과 같은 경우 아이들 ==이 0