Proper way to use onExit() function

Proper way to use onExit() function
0.0 0


I added clearing operations (and related operation even it’s not meant to clear at all) for resource. See my code below.

`onExit:function () {
// stop playing background music
if(util.isPC() && !cc.AudioManager.sharedEngine().isBackgroundMusicPlaying())
cc.AudioManager.sharedEngine().playBackgroundMusic(res_game_theme_music, true);

// remove sprite frames from cacahe

Note: util is my utility class.

I did this because I want it to be transparent to a manager class above, that there’s no need to explicitly call a particular function for such scene to be completely clean and ready for another scene to move on. Example is that stopping a bg music and remove sprite frames from cache in order to prevent interfere in “key” naming inside big sheet of multiple scene, and also sound stopped playing for bg if not stop first and play after.

When I tested above code, it seems that after switched into another scene the objects from the previous scene are still in active and got called in the update loop. Example from what I tested is that objects added into a layer in the previous scene will still be alive and effect the touch event even if you already switched into another scene already, not all of objects in the previous scene removed.

So my question is what’s the best and proper to use onExit* function, and also when is the right and proper time to useonExit()* function.
Thanks ahead !


I think you need in your code add this code:


Thanks for pointing out dingping. I will test it out and get you back asap.