问题
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