actionscript 3 - Does addEventListener (and removeEventListener) stack? -


given below:

object.addeventlisten(eventtype01,handler01); object.addeventlisten(eventtype01,handler01);  object.removeeventlistener(eventtype01,handler01); 

how many event listeners eventtype01 on object? 1 or zero?

zero. if call addeventlistener, using exact same arguments more once subsequent calls after first "silently fail." call add many times want, single remove wipe listener away.

edit: thing keep in mind there's no penalty calling multiple identical removeeventlistener() functions on same object, aside needless performance overhead. no deadly error occur, "silently fail" same way repeated identical addeventlistener calls will.

edit 2: answer @thomasm :: if listener "fires twice" not have same listener. try putting quick , dirty code on frame 1 in fla has 1 square movieclip child:

import flash.events.* function foo(e):void{     trace("hi"); }  this.addeventlistener(mouseevent.click,foo); this.addeventlistener(mouseevent.click,foo); this.addeventlistener(mouseevent.click,foo); 

notice output when click movieclip precisely 1 trace action.

now add line end of code

this.removeeventlistener(mouseevent.click,foo); 

even though added foo listener click 3 times, 1 call kills listener completely.

so if you're experiencing issue "the same listener" fire twice you're doing this:

this.addeventlistener(mouseevent.click, function(e){     trace("hi"); });  this.addeventlistener(mouseevent.click, function(e){     trace("hi"); }); 

that "fire twice" because listener functions are not same. perform same actions not reference identical positions in memory. not point same function object. furthermore highly bad practice because have no way remove listeners. how it? have no reference them.


Comments