flash as3 check event listener

≡放荡痞女 提交于 2020-12-29 12:05:39

问题


Is their a way to check if an event listener already exists to remove it?

stage.addEventListener(MouseEvent.CLICK, clickdownfunction);

Basically, I want to remove the listener, but sometimes it has already been removed, so I want to check if it exists and if it does, then remove it.

Is this possible?


回答1:


Check out the hasEventListener() function from

https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/IEventDispatcher.html#hasEventListener()

I am not really sure though why you want to do that check. Removing non existant listeners won't make Flash drop exceptions or errors, thus the check is just adding unneccessary overhead.




回答2:


you can't check if a specific function is registered as a listener, you can though check if a type is registered. This can be done with this:

hasEventListener(type:String):Boolean

Alternatively you can just call removeEventListener, if it's not registered it'll just ignore the call.

Hope that helps,




回答3:


Here's the code you need to remove the event listener only if it is active:

if(stage.hasEventListener(MouseEvent.CLICK))
     stage.removeEventListener(MouseEvent.CLICK, clickdownfunction);



回答4:


You can also use Fingers:

 on(stage).click -= clickdownfunction;


来源:https://stackoverflow.com/questions/5157768/flash-as3-check-event-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!