2014-03-04 34 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

相关问题