2011-09-15 105 views
0

我正在为我的Flash应用程序(AS3和Flash CS5)开发drag'n'clone功能。克隆创建完美,但是当我尝试拖动最近创建的克隆时,应用程序会创建一个新克隆(并允许我拖动它)。事件不会从AS3中的侦听器中删除

我想删除此行为:克隆只应拖放,不克隆。我的代码是:

public class Car extends MovieClip 
    { 

     // imports... 

     public function Car() 
     { 
      addListeners(); 
     } 

     private function addListeners():void 
     { 
      this.addEventListener(MouseEvent.MOUSE_DOWN,clone); 
     } 

     private function clone(e:MouseEvent):void 
     { 
      // Clone the object 
      var newcar = new e.target.constructor; 
      newcar.graphics.copyFrom(this.graphics); 
      newcar.x = this.x; 
      newcar.y = this.y; 

      this.parent.addChild(newcar); 

      newcar.addEventListener(MouseEvent.MOUSE_MOVE,dragCar); 
      newcar.addEventListener(MouseEvent.MOUSE_UP,dropCar); 
     } 

     private function dragCar(e:MouseEvent):void 
     { 
      e.target.startDrag(); 
     } 

     private function dropCar(e:MouseEvent):void 
     { 
      e.target.stopDrag(); 

        // This line doesn't remove the event, and I don't know why 
      e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN,clone); 

      e.target.removeEventListener(MouseEvent.MOUSE_MOVE, dragCar); 
      e.target.removeEventListener(MouseEvent.MOUSE_UP,dropCar); 

     } 

    } 

我希望有人能帮助我。谢谢!

回答

1

这里因为你是在克隆功能创造汽车的新实例,新车对象的构造函数被调用这反过来调用addListeners()和克隆具有MOUSE_DOWN事件侦听器再次克隆的克隆。这就是为什么你有你描述的问题。

为了避免这种情况,你需要在你克隆功能,添加以下行(这不起作用见下面编辑

newcar.removeEventListener(MouseEvent.MOUSE_DOWN,clone); 

这个来自克隆(新)汽车实例移除克隆事件监听器并避免再次克隆。

此外,要开始拖动你应该在MOUSE_DOWN而不是MOUSE_MOVE。

更新
好,我看,MOUSE_DOWN不会再次呼吁克隆,所以你需要使用MOUSE_MOVE。因此,在这种情况下,我将删除侦听器正文中的MOUSE_MOVE侦听器,以便仅调用一次。

更新
这似乎删除事件侦听器。

newcar.removeEventListener(MouseEvent.MOUSE_DOWN, newcar.clone); 

你必须参考newcar实例的clone方法newcar.clone删除事件侦听器。

工作守则
下面的代码工作得很好

package{ 
    import flash.display.MovieClip; 
    import flash.events.*; 
    public class Car extends MovieClip 
    { 
     public function Car() 
     { 
      addListeners(); 
     } 

     private function addListeners():void 
     { 
      this.addEventListener(MouseEvent.MOUSE_DOWN,clone); 
     } 

     private function clone(e:MouseEvent):void 
     { 
      // Clone the object 
      var newcar = new e.target.constructor; 
      newcar.graphics.copyFrom(this.graphics); 
      newcar.x = this.x; 
      newcar.y = this.y; 

      this.parent.addChild(newcar); 

      // remove the clone listener 
      newcar.removeEventListener(MouseEvent.MOUSE_DOWN, newcar.clone); 

      // add dragging listeners 
      newcar.addEventListener(MouseEvent.MOUSE_MOVE, dragCar); 
      newcar.addEventListener(MouseEvent.MOUSE_UP, dropCar); 

      // this is used for dragging the clone 
      newcar.addEventListener(MouseEvent.MOUSE_DOWN, dragCar); 
     } 

     private function dragCar(e:MouseEvent):void 
     { 
      // if a MOUSE_MOVE event listener is registered remove it 
     if(e.target.hasEventListener(MouseEvent.MOUSE_MOVE)) 
      e.target.removeEventListener(MouseEvent.MOUSE_MOVE, dragCar); 
      e.target.startDrag(); 
     } 

     private function dropCar(e:MouseEvent):void 
     { 
      e.target.stopDrag(); 

      // do not remove the listener if you want the clone to be dragable 
      // e.target.removeEventListener(MouseEvent.MOUSE_UP,dropCar); 
     } 
    } 
} 

保持克隆dragable
对于这个在上面的代码中,我添加了一个MOUSE_DOWN事件侦听器拖动克隆

newcar.addEventListener(MouseEvent.MOUSE_DOWN, dragCar); 

并注释掉了e.target.removeEventListener(MouseEvent.MOUSE_UP,dropCar); in dropCar

此外,在删除MOUSE_MOVE侦听器之前,检查是否存在或不存在,因为此函数稍后也将由MOUSE_DOWN调用。

+0

因此,我正在消除事件,但它仍然存在。我该如何解决它?谢谢! –

+0

看看我的答案的更新,我们需要参考'newcar'实例的'clone'函数作为'newcar.clone' – danishgoel

+0

感谢队友,但现在我不能拖放克隆,我想做到这一点。我该怎么做?非常感谢。 –