12

If I have an object that calls

addEventListener(Event.ENTER_FRAME, update);  
addEventListener(Event.ENTER_FRAME, update);

will that add 2 listeners?

Pup
  • 10,236
  • 6
  • 45
  • 66

3 Answers3

15

Nope, they won't, so update will only be called once when the event triggers.

quoo
  • 6,237
  • 1
  • 19
  • 36
  • That's awesome! I have a function that adds event listeners that I need to call multiple times. I was concerned about this causing some bad side effects. – user359519 Aug 23 '11 at 13:30
  • 1
    does this apply to Anonymous Functions? – ThorSummoner May 08 '14 at 17:39
  • 4
    @ThorSummoner if you define an anonymous function more than once (e.g. in a loop), a new function is defined every time. So it won't work with anonymous functions unless you create the function once, save it in a variable and pass the variable to your `addEventListener` call. That's the best practice anyway. – tomekwi Jan 09 '15 at 11:49
3

Depends on what you're attaching the listeners to. If you attach to movieClipX and to movieClipY, you'll have two listeners,so if one of the mcs is removed you'll still have the other listener. If you attach the same listener to the same object twice,it'll behave as a single listener.

Jorge Guberte
  • 10,464
  • 8
  • 37
  • 56
1

Also, to complete on the already provided answers, if you do:

addEventListener(Event.ENTER_FRAME, update1);  
addEventListener(Event.ENTER_FRAME, update2);

Then it will execute BOTH functions. The later addEventLister will NOT overwrite the previous one, but add to the existing listeners, as the name of the method implies (except if the listener function was already added, in which case it will do nothing, as already stated in the accepted answer).

OMA
  • 3,442
  • 2
  • 32
  • 39