2014-02-19 21 views
0

我試圖刪除編程添加的動畫片段,當它們與某個對象發生碰撞時。當他們這樣做時,他們就消失了。但消失後,我得到了#1009錯誤。As3:以編程方式刪除命中測試中添加的子項給出錯誤#1009

在95行,這是

if (this.x > stage.stageWidth + 2 - (stage.stageWidth - (this.width /2)) && this.x < stage.stageWidth - (this.width /2))" 

有時在59,這是

if (this.x >= stage.stageWidth - 20)" 

爲什麼它一直試圖運行線95(有時)59錯誤點?我刪除了eventlistener。

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.*; 
    import flash.utils.*; 
    public class Worker extends MovieClip 
    { 

     private var _root:MovieClip; 
     private var actualRange:Number; 
     private var reachedGoal:Boolean = false; 
     private var b:uint; 
     private var destination:Number; 
     private var goRight:Boolean = true; 
     private var goNowhere:Boolean = true; 
     private var yDir:Number; 
     private var yNumber:Number; 

      public function Worker() 
      { 
       addEventListener(Event.ADDED, beginClass); 
       addEventListener(Event.ENTER_FRAME, loop); 
       //defining _root as the document root 

       actualRange =(Math.floor(Math.random() * (520 - 80 + 1)) + 80); 

       b = setInterval(startInc, 3000); 
      } 


     private function beginClass(event:Event):void{ 
      //defining _root as the document root 
      _root = MovieClip(root); 
     } 

      private function loop(event:Event):void 
      { 
       if (this.hitTestObject(_root.killGun)) 
       { 
        { 
         removeEventListener(Event.ENTER_FRAME, loop); 
         _root.removeChild(this); 
        } 
       } 

       if (reachedGoal == true) 
       { 

        if (goNowhere == true) 
         { 
          gotoAndStop(1); 
         } 
//Boundaries 
        if (this.x <= 20) 
        { 
         goRight = true; 
         goNowhere = false; 
        } 
        if (this.x >= stage.stageWidth - 20) 
        { 
         goRight = false; 
         goNowhere = false; 
        } 

        if (this.y >= stage.stageHeight - 20) 
        { 
         yDir = 0 
        } 

        if (this.y <= 20) 
        { 
         yDir = 1 
        } 
       } 

//Intro walk 
       if (reachedGoal == false) 
       { 
        if (this.x < actualRange) 
         { 
          gotoAndStop(4); 
          this.x += 3 
         } 

        if (this.x >= actualRange) 
         { 
          reachedGoal = true; 
          gotoAndStop(1); 
         } 
       } 


//Main walk 
       if (this.x > stage.stageWidth + 2 - (stage.stageWidth - (this.width /2)) && this.x < stage.stageWidth - (this.width /2)) 
       { 
        if (goNowhere == false) 
        { 
          if (goRight == true) 
          { 
           this.x += 1; 
           gotoAndStop(2); 

           //Y value 
          } 
          if (goRight == false) 
          { 
           if (this.x < stage.stageWidth - this.width /2) 
           this.x -= 1 
           gotoAndStop(3); 

           //Y value 

           if (yNumber == 1) 
           {         
            if(yDir == 0) 
            { 
             this.y -= 1; 
            } 
            if (yDir == 1) 
            { 
             this.y += 1; 
            } 
           } 

          } 

        } 

       } 


      } 


      //Timer functions 
      private function completePlay() 
      { 
       clearInterval(b); 
      } 

      private function startInc() 
      { 
        var moveNumber:Number = Math.floor(Math.random() * 20); 

        if (moveNumber == 1) 
        { 
         destination = Math.floor(Math.random() * 50); 
         yDir = Math.floor(Math.random() * 2); 
         yNumber = Math.floor(Math.random() * 4); 

         goRight = true; 
         goNowhere = false; 
        } 

        else if (moveNumber == 2) 
        { 
         destination = Math.floor(Math.random() * 50);  
         yDir = Math.floor(Math.random() * 2); 
         yNumber = Math.floor(Math.random() * 4); 

         goRight = false; 
         goNowhere = false; 
         gotoAndStop(3); 
        } 

        else 
        { 
         goNowhere = true; 
        } 

      }   


//End 

    } 

} 

回答

0

的錯誤發生,因爲你的eventListener的函數中取出eventListener和你刪除對象從它的父,仍然在處理數據該對象不再包含或已被取消。您應該添加一個boolean以確定hitTest是否已經發生,然後不再處理該循環。例如:

private function loop(event:Event):void 
    { 
    var objectHit:Boolean = false; 

    objectHit = this.hitTestObject(_root.killGun); 

    if(!objectHit) { 
     if (reachedGoal == true) 
     { 

      if (goNowhere == true) 
      { 
       gotoAndStop(1); 
      } 
      //Boundaries 
      if (this.x <= 20) 
      { 
       goRight = true; 
       goNowhere = false; 
      } 
      if (this.x >= stage.stageWidth - 20) 
      { 
       goRight = false; 
       goNowhere = false; 
      } 

      if (this.y >= stage.stageHeight - 20) 
      { 
       yDir = 0 
      } 

      if (this.y <= 20) 
      { 
       yDir = 1 
      } 
     } 

     //Intro walk 
     if (reachedGoal == false) 
     { 
      if (this.x < actualRange) 
       { 
       gotoAndStop(4); 
       this.x += 3 
      } 

      if (this.x >= actualRange) 
      { 
       reachedGoal = true; 
       gotoAndStop(1); 
      } 
     } 
    } 

    if (objetHit) 
    { 
     removeEventListener(Event.ENTER_FRAME, loop); 
     _root.removeChild(this); 
    } 
} 

你也應該刪除Event.ADDED聽衆一旦對象已被添加。

private function beginClass(event:Event):void{ 
     //defining _root as the document root 
     _root = MovieClip(root); 
     this.removeEventListener(Event.ADDED, beginClass); 
    } 
+0

非常感謝!它完美無瑕! – user3123633

+0

哥們沒問題 –

0

難道是循環的其餘部分試圖仍然運行?

嘗試命中後破環了:

if (this.hitTestObject(_root.killGun)) 
{ 
    removeEventListener(Event.ENTER_FRAME, loop); 
    _root.removeChild(this); 
    return; 
}