Skip to main content
Anonymous507
Known Participant
July 28, 2014
Answered

callback method vs event listeners

  • July 28, 2014
  • 2 replies
  • 786 views

I have developing a game in AS 3 with MVC pattern. In my document class, I have created around 15 event listeners to update the game view.

My question is instead of event listeners can I use call back function?.

I have tested the sample file by replacing the addeventlisteners with callback function. It is working fine.

I think compare to eventlisteners, call back will took less memory only ().

Any suggestion please?

thanks in advance

This topic has been closed for replies.
Correct answer Amy Blankenship

Callback functions are more performant. Event listeners allow for a higher degree of decoupling in some cases, because you often don't need to know where the thing is you're listening to and the thing dispatching the event doesn't need to know or care who, if anyone, is listening. And as soon as you need for more than one thing to listen, callbacks get more complex (see AS3 Signals).

With either callbacks or event listeners, keep in mind you are handing a reference to the listening object to the dispatching object. Make sure to clean everything up when the listening object is no longer in use, or you can wind up with memory leaks.

2 replies

Amy Blankenship
Amy BlankenshipCorrect answer
Legend
July 28, 2014

Callback functions are more performant. Event listeners allow for a higher degree of decoupling in some cases, because you often don't need to know where the thing is you're listening to and the thing dispatching the event doesn't need to know or care who, if anyone, is listening. And as soon as you need for more than one thing to listen, callbacks get more complex (see AS3 Signals).

With either callbacks or event listeners, keep in mind you are handing a reference to the listening object to the dispatching object. Make sure to clean everything up when the listening object is no longer in use, or you can wind up with memory leaks.

kglad
Community Expert
Community Expert
July 28, 2014

yes, you can use call back functions.