我想删除侦听器,但无法将其删除。我使用的方法:board.removeEventListener(MouseEvent.MOUSE_MOVE,mouseDrag);
var board:UIComponent=new UIComponent();
board.addEventListener(MouseEvent.MOUSE_MOVE, function(event:MouseEvent):void
{
mouseDrag(event,board);
});
private function mouseDrag(event:MouseEvent,board:UIComponent):void
{
var x:Number=rectX + (event.stageX - mouseX);
var y:Number=rectY + (event.stageY - mouseY); board.x=x;
board.y=y;
}
flex AS3.0如何删除监听器?
答案 0 :(得分:3)
您没有删除与首先添加的功能相同的功能。您附加了一个执行mouseDrag
的匿名函数,并且您尝试删除mouseDrag
本身。
您的情况有一些解决方案。
board.addEventListener(MouseEvent.MOUSE_MOVE, mouseDrag);
private function mouseDrag(event:MouseEvent):void {
var board:UIComponent = event.currentTarget as UIComponent;
board.removeEventListener(MouseEvent.MOUSE_MOVE, mouseDrag);
...
}
var anonymousFunction:Function = function(event:MouseEvent):void {
mouseDrag(event,board);
};
board.addEventListener(MouseEvent.MOUSE_MOVE, anonymousFunction);
如果您仍然可以访问该引用,则可以正确删除侦听器:
board.removeEventListener(MouseEvent.MOUSE_MOVE, anonymousFunction);