2014-03-04 38 views
0

我有一個忍者星級。As3:_root.removeChild(this)不適用於數組特定的情況下?

在循環功能,我有:

private function loop (event:Event):void 
    {   
    trace(this); 

     for (var i:int=0; i<_root.guardArray.length; i++) 
     { 
      //if movieClip at position [i] (enemy) hits this 
      if (_root.guardArray[i].hitTestObject(this)) 
      { 
       if(this.hitTestObject(_root.guardArray[i].hitbox)) 
       { 
        _root.guardArray[i].killThis(); 
        _root.removeChild(this); 
        removeEventListener(Event.ENTER_FRAME, loop); 
       } 
      } 
     } 

      y-=Math.cos(rotation/-180*Math.PI)*(ninjaStarSpeed); 
      x-=Math.sin(rotation/-180*Math.PI)*(ninjaStarSpeed); 


     if(this.x < 0 || this.x > _root.stagewidth || this.y > _root.stageheight || this.y < 0) 
     { 
      _root.removeChild(this); 
      removeEventListener(Event.ENTER_FRAME, loop); 
     } 
    } 
} 

的飛鏢成功刪除本身沒有任何錯誤,當它熄滅的畫面。

但是,當它打到警衛時,它會自行移除,但會給我一個#2025錯誤@第40行!

這是第40行:_root.removeChild(this); - 它是數組碰撞檢查的一部分。

閃存錯誤或我正在做什麼非常錯誤?

回答

1

是的,你正在做的事情不對的,因爲錯誤的;)

的代碼段爲您提供:

private function safeRemove():void{ 
    if(this.parent != null){ 
     this.parent.removeChild(this); 
    } 
} 

此方法添加到NinjaStar類,並使用它。因此,代碼40個行會看起來像

//And don't forget not only kill guard, but also clear reference on him from the guardArray. 
_root.guardArray[i].killThis(); 
safeRemove(); 
removeEventListener(Event.ENTER_FRAME, loop); 
+0

THANK YOU !!!!!!!!!!!!!!!!!!!!!!!! :D – user3123633

+0

我愛你這麼多! :d – user3123633

相關問題