有没有办法检查事件监听器是否已经存在并将其删除?
stage.addEventListener(MouseEvent.CLICK, clickdownfunction);
基本上,我想要移除监听器,但有时它已经被移除了,所以我想检查它是否存在,如果存在,则将其移除。
这个可能吗?
有没有办法检查事件监听器是否已经存在并将其删除?
stage.addEventListener(MouseEvent.CLICK, clickdownfunction);
请查看https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/IEventDispatcher.html#hasEventListener()中的hasEventListener()
函数。
然而,我不确定为什么您想要进行此检查。删除不存在的侦听器并不会使Flash出现异常或错误,因此该检查只会增加不必要的开销。
你无法检查特定函数是否已注册为监听器,但你可以检查类型是否已注册。可以通过以下方式完成:
hasEventListener(type:String):Boolean
如果没有注册事件,你可以直接调用removeEventListener
,它会忽略这个调用。
希望能对你有所帮助。
如果事件监听器处于活动状态,您需要的代码是将其删除:
if(stage.hasEventListener(MouseEvent.CLICK))
stage.removeEventListener(MouseEvent.CLICK, clickdownfunction);