flex AS3.0如何删除Listener?

时间:2013-02-05 09:01:54

标签: flex

我想删除侦听器,但无法将其删除。我使用的方法: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如何删除监听器?

1 个答案:

答案 0 :(得分:3)

您没有删除与首先添加的功能相同的功能。您附加了一个执行mouseDrag的匿名函数,并且您尝试删除mouseDrag本身。

您的情况有一些解决方案。

1。抛弃匿名函数并使用event.currentTarget

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);
    ...
}

2。将匿名函数放在变量中,以便您有一个引用

var anonymousFunction:Function = function(event:MouseEvent):void {
    mouseDrag(event,board);             
};
board.addEventListener(MouseEvent.MOUSE_MOVE, anonymousFunction);

如果您仍然可以访问该引用,则可以正确删除侦听器:

board.removeEventListener(MouseEvent.MOUSE_MOVE, anonymousFunction);