2013-02-21 72 views
0

我有一名學生正在AS3中進行塔防遊戲,並且有一個難題讓我難住。他正在使用hitTestObject來改變movieClip移動的方向。 movieClip擁有自己的時間線,其中包含面向對象所面向的不同方向的幀,以及包含對象行爲代碼的鏈接.as文件。AS3 gotoAndStop導致對象自行刪除

當他調用gotoAndStop來更改movieClip的內部框架時,將移除的事件被觸發,但對象停留在屏幕上並不再移動。

我所有的搜索都能找到關於刪除對象的答案,但我還沒有看到任何有關防止刪除對象的問題。

下面的代碼是通過在。作爲類文件MovieClip對象的ENTER_FRAME事件觸發的循環:

private function eFrame(event:Event):void 
    { 

     if (_root.isPaused == false) 
     { 
      //MOVING THE ENEMY 
      this.x += speed * xDir; 
      this.y -= speed * yDir; 
      if (health <= 0) 
      { 
       _root.currency += 4; 

       this.parent.removeChild(this); 
      } 
      if (this.x > 770) 
      { 
       this.parent.removeChild(this); 
       _root.health -= 10; 
       _root.gotHit = true; 
      } 
      //checking if touching any invisible markers 
      for (var i:int=0; i<_root.upHolder.numChildren; i++) 
      { 
       //the process is very similar to the main guy's testing with other elements 
       var upMarker:DisplayObject = _root.upHolder.getChildAt(i); 
       if (hitTestObject(upMarker)) 
       { 
        yDir = 1; 
        xDir = 0; 
        this.gotoAndStop(3); 


       } 
      } 
      for (i=0; i<_root.downHolder.numChildren; i++) 
      { 
       //the process is very similar to the main guy's testing with other elements 
       var downMarker:DisplayObject = _root.downHolder.getChildAt(i); 
       if (hitTestObject(downMarker)) 
       { 
        yDir = -1; 
        xDir = 0; 
        this.gotoAndStop(7); 

       } 
      } 

      for (i=0; i<_root.rightHolder.numChildren; i++) 
      { 
       //the process is very similar to the main guy's testing with other elements 
       var rightMarker:DisplayObject = _root.rightHolder.getChildAt(i); 
       if (hitTestObject(rightMarker)) 
       { 
        yDir = 0; 
        xDir = 1; 
        this.gotoAndStop(6); 
       } 
      } 
      for (i=0; i<_root.leftHolder.numChildren; i++) 
      { 
       //the process is very similar to the main guy's testing with other elements 
       var leftMarker:DisplayObject = _root.leftHolder.getChildAt(i); 
       if (hitTestObject(leftMarker)) 
       { 
        yDir = 0; 
        xDir = -1; 
        this.gotoAndStop(2); 

       } 
      } 
     } 
    } 
    private function remove(event:Event):void 
    { 
     trace("remove"); 
     removeEventListener(Event.ENTER_FRAME, eFrame); 
     _root.enemiesLeft -= 1; 

    } 
} 

當gotoAndStop行執行,的動畫片段改變該幀,然後將代碼跳到直接傳遞給由REMOVED事件觸發的函數。

有沒有人有一個想法,爲什麼REMOVED事件可能由此代碼觸發?

謝謝你的幫助。

回答

1

如果我沒有弄錯,REMOVED事件是由MovieClip或Sprite中包含它的階段中刪除的任何內容觸發的。尤其是對於具有動畫的動畫片段,每次都會刪除並添加,例如動畫的某些部分在時間軸上或關鍵幀處結束。

Event.REMOVED_FROM_STAGE僅在容器本身從舞臺上移除時纔會調度。也許這是造成你的困惑?我無法從您的代碼示例中看到您正在監聽的事件類型。

+0

謝謝。將我的事件監聽器從REMOVED更改爲REMOVED_FROM_STAGE解決了這個問題。 – wrTechTeacher 2013-02-22 16:54:29

0

你在哪裏添加刪除監聽器?

如果沒有更多的信息,我會猜測您正在偵聽動畫中的剪輯,並且它並不存在於所有幀中(或者更可能是 - 實例正在換出另一個相同的幀,這可能會發生,這取決於你添加關鍵幀的順序,月球的對齊和電離層的波動,最簡單的方法是通過移除所有關鍵幀然後重新創建它們,然後再不使用閃光燈pro)。