删除事件监听器 [英] removing event listener

查看:160
本文介绍了删除事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学会了addEventLister传递值的方法。这里是code:

 的(VAR我:UINT = 0; I< asteroids.length;我++)
{

     小行星[I] .X =的Math.random()* 450;
     小行星[I] .Y =的Math.random()* 450;
     小行星[I] .addEventListener(侦听MouseEvent.MOUSE_UP,功能(E:的MouseEvent){
         changeValue(即otherArguments);
     });

}



公共职能changeValue(事件:的MouseEvent,otherArguments:对象):无效
{

    playSound(a注解);
    跟踪(event.currentTarget);

}
 

但有关如何从

移除事件监听器没有解释

 小行星[I] .addEventListener(侦听MouseEvent.MOUSE_UP,功能(E:的MouseEvent){
     changeValue(即otherArguments);
 });
 

解决方案

那是因为你不能解开一个匿名函数,如果你没有一个参考吧。如果你愿意,你可以始终保持一个参考各地的:

  asteroidsMouseUpHandler =功能(E:的MouseEvent){
    changeValue(即otherArguments);
};

小行星[I] .addEventListener(侦听MouseEvent.MOUSE_UP,asteroidsMouseUpHandler);

小行星[I] .removeEventListener(侦听MouseEvent.MOUSE_UP,asteroidsMouseUpHandler);
 

当然,如果这些事情都发生在不同的成员函数,那么你就需要定义一个成员变量( VAR asteroidsMouseUpHandler:功能)。在这一点上,你可能只是想,而不是将其创建为命名的功能。你也想​​只希望一次做到这一点(而不是在一个循环中),以避免弄错你参考。说真的,我显示此code,仅供参考。

思考这个进一步,你可能会做这个,因为 otherArguments 是针对特定小行星[I] 实例,这样你实际上是创建一个新的匿名函数每次你把它挂。在这种情况下,如果这是你真正想做的事情,你可以跟踪你的匿名函数在字典中:

  VAR asteriodsCallbacks:字典=新词典();
 

然后,您可以跟踪在那里的功能:

  asteroidsCallbacks [小行星[I] = asteroidsMouseUpHandler;
小行星[I] .addEventListener(侦听MouseEvent.MOUSE_UP,asteroidsMouseUpHandler);
 

然后当你要脱钩,你可以看看它:

 小行星[I] .removeEventListener(侦听MouseEvent.MOUSE_UP,asteroidsCallbacks [小行星[I]);
 

当然,我忽略了存在检查简单。你必须补充一点为好。

I have learned this method of passing values with addEventLister. Here is the code:

for (var i:uint = 0; i < asteroids.length; i++)
{

     asteroids[i].x = Math.random() * 450;
     asteroids[i].y = Math.random() * 450;
     asteroids[i].addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent){
         changeValue(e, otherArguments);
     });

}



public function changeValue(event:MouseEvent, otherArguments:Object):void
{

    playSound(anote);
    trace(event.currentTarget);

}

but there is no explanation about how to remove the event listener from

  asteroids[i].addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent){
     changeValue(e, otherArguments);
 });

解决方案

That is because you cannot unhook an anonymous function if you don't have a reference to it. If you'd like, you can always keep a reference around:

asteroidsMouseUpHandler = function(e:MouseEvent){
    changeValue(e, otherArguments);
};

asteroids[i].addEventListener(MouseEvent.MOUSE_UP, asteroidsMouseUpHandler);

asteroids[i].removeEventListener(MouseEvent.MOUSE_UP, asteroidsMouseUpHandler);

Of course, if these things are happening in separate member functions, then you need to define a member variable (var asteroidsMouseUpHandler:Function). At that point, you might just want to create it as a named function instead. You'd also only want to do this once (not in a loop) to avoid clobbering your reference. Really, I am showing this code for illustrative purposes only.

Thinking about this further, you might be doing this because the otherArguments is specific to the particular asteroids[i] instance so that you are actually creating a new anonymous function every time you hook it. In that case, if that is what you really want to do, you can keep track of your anonymous functions in a dictionary:

var asteriodsCallbacks:Dictionary = new Dictionary();

Then, you can keep track of the functions there:

asteroidsCallbacks[asteroids[i]] = asteroidsMouseUpHandler;
asteroids[i].addEventListener(MouseEvent.MOUSE_UP, asteroidsMouseUpHandler);

And then when you want to unhook, you can look it up:

asteroids[i].removeEventListener(MouseEvent.MOUSE_UP, asteroidsCallbacks[asteroids[i]]);

Of course, I am ignoring existential checks for simplicity. You'd have to add that as well.

这篇关于删除事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆