Storyboard - When to remove eventListeners

I have been playing with a few simple storyboard scenes. The Sample code seems to remove event listeners when the scene is destroyed, and add listeners on “enterScene”

With my tests, if I did not purge the previous scene then I would end up doubling up on the events. Each time you switch scene, these would then double again.

So, I am asking

  1. Is it better to remove event listeners on “exitScene” rather than “destroyScene”

  2. Am I increasing memory usage each time I swap scenes without purging (if I swap between 2 scenes only)

Thanks

  1. I consider enter+exitScene and create+destroyScene to be paired. Generally anything I do in enterScene I will undo in exitScene, and anything I do in createScene I will undo in destroyScene. Listeners attached to objects don’t need to be removed manually if the object is deleted, the listener will be removed automatically. So for me, the only non-object listener I will usually add in enterScene will be the enterFrame listener, which I remove in exitScene. With a bit of work, you could even set up a function that automatically cancels functions on any scene change, but I’m afraid I haven’t got time to work that out at the moment.

  2. If you do scene1->scene2->scene1 I believe that scene1 will still be in memory, unless you have purged it of course. So in that sense you may be using more memory, however it may be that scene2 is a pause menu, so you wouldn’t want to delete scene1 because you’ll be going back to it afterwards. The real issue is if your memory usage keeps going up and up every time you change between the 2 scenes. 

Hi Alan,

Thanks for your answer - it seems to confirm my thoughts about not removing listeners on destroy but to use exit instead.

I didn’t notice an increase in memory usage if I just flip back and forth between two scenes without calling storyboard.purge I perhaps wouldn’t need to actually do this in a real world app, but it’s useful to know that the memory is handled correctly.

Thanks again

  1. I consider enter+exitScene and create+destroyScene to be paired. Generally anything I do in enterScene I will undo in exitScene, and anything I do in createScene I will undo in destroyScene. Listeners attached to objects don’t need to be removed manually if the object is deleted, the listener will be removed automatically. So for me, the only non-object listener I will usually add in enterScene will be the enterFrame listener, which I remove in exitScene. With a bit of work, you could even set up a function that automatically cancels functions on any scene change, but I’m afraid I haven’t got time to work that out at the moment.

  2. If you do scene1->scene2->scene1 I believe that scene1 will still be in memory, unless you have purged it of course. So in that sense you may be using more memory, however it may be that scene2 is a pause menu, so you wouldn’t want to delete scene1 because you’ll be going back to it afterwards. The real issue is if your memory usage keeps going up and up every time you change between the 2 scenes. 

Hi Alan,

Thanks for your answer - it seems to confirm my thoughts about not removing listeners on destroy but to use exit instead.

I didn’t notice an increase in memory usage if I just flip back and forth between two scenes without calling storyboard.purge I perhaps wouldn’t need to actually do this in a real world app, but it’s useful to know that the memory is handled correctly.

Thanks again