Actionscript 3 事件不是';在AS3中,未从侦听器中删除
我正在为我的flash应用程序(AS3和flash CS5)开发一个拖放克隆功能。克隆创建得非常完美,但当我尝试拖动最近创建的克隆时,应用程序会创建一个新克隆(并允许我拖动它) 我想删除这种行为:只能拖放克隆,不能克隆。我的代码是:Actionscript 3 事件不是';在AS3中,未从侦听器中删除,actionscript-3,flash-cs5,Actionscript 3,Flash Cs5,我正在为我的flash应用程序(AS3和flash CS5)开发一个拖放克隆功能。克隆创建得非常完美,但当我尝试拖动最近创建的克隆时,应用程序会创建一个新克隆(并允许我拖动它) 我想删除这种行为:只能拖放克隆,不能克隆。我的代码是: public class Car extends MovieClip { // imports... public function Car() { addListeners();
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);
}
}
我希望有人能帮助我。谢谢 在这里,当您在克隆函数中创建一个新的Car实例时,将调用该新Car对象的构造函数,该构造函数依次调用
addListeners()
,克隆具有一个鼠标按下事件监听器,该监听器将再次克隆克隆。这就是为什么你有你描述的问题
为了避免这种情况,您需要在克隆函数中添加以下行(这不起作用,请参见下面的编辑)
这将从克隆的(新的)Car实例中删除克隆事件侦听器,并避免再次克隆
此外,要开始拖动,您应该在鼠标向下而不是移动鼠标时进行拖动
更新好的,我明白了,克隆不会再次调用MOUSE\u DOWN,所以您需要使用MOUSE\u MOVE。因此,在本例中,我将删除侦听器主体中的MOUSE\u MOVE侦听器,以便只调用一次 更新
这似乎删除了事件侦听器
newcar.removeEventListener(MouseEvent.MOUSE_DOWN, newcar.clone);
必须参考newcar实例的clone方法newcar.clone
,才能删除事件侦听器
newcar.removeEventListener(MouseEvent.MOUSE_DOWN, 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);
}
}
}
保持克隆可拖动在上面的代码中,我添加了一个鼠标向下事件监听器,用于拖动克隆
newcar.addEventListener(MouseEvent.MOUSE_DOWN, dragCar);
并注释掉e.target.removeEventListener(MouseEvent.MOUSE\u UP,dropCar)代码>在dropCar中
在移除鼠标之前,还添加了一个检查MOUSE\u MOVE listener是否存在,因为MOUSE\u DOWN稍后也会调用此函数。因此,我正在移除事件,但它仍然存在。我怎样才能解决它?谢谢看看我答案的更新,我们需要将newcar
实例的clone
功能称为newcar.clone
谢谢,伙计,但是现在我不能拖放克隆,我想做。我怎么做?非常感谢。我很好奇为什么要删除事件侦听器以支持拖放。我以为你不想把克隆人拖走。我将更新我的答案,以便可以拖动克隆。