2013-02-21 77 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)。