Does composer automatically handle removal of display objects, timers, sounds, eventlisteners when scene:hide is called?

My game consists of mainly two scenes. The mainmenu and the game itself. Which is just one scene. So users will spend 95% of their time in game.lua. Considering this, is it wise to remove everything when the user goes to the mainmenu for a short amount of time. I would have to create the whole game scene again a couple of seconds later when the user returns.

Is this process automatic? Or do I have to remove the objects?

I think that if you put it in the composer-provided group (self.view, aliased to scenegroup) it does work, but I don’t think you can guarantee other things, e.g. removal of event listeners.

Make sure you add all display objects to the sceneGroup. When a scene is removed, all objects within sceneGroup will be removed. Add objects with the following code: sceneGroup:insert(object).

Scenes are not removed from memory unless you remove them with the code: composer.removeScene(scene). You can not remove the active scene. So, if game.lua is the active scene, composer.removeScene(game) will not work. A workaround is to set up a blank scene. Let’s say its game over and you want you have a button to play again. If you simply load game.lua ontop of itself, you will have two instances of the scene loaded on the screen. It won’t work. Instead, have the play again button point to a blank scene. Within the blank scene, add a function which handles the removal of the game scene and the reloading of it. Then, set up a timer to run the function. It would look something like this.

function reLoadGame()

     composer.removeScene(game)

     composer.gotoScene(game)

end 

timer.performWithDelay(1000, reLoadGame)

You will need to handle widget Button and event listener removal on your own, as they are not part of scene cleanup. This can be handled within the scene:destroy() function.

Composer only manages display objects.  You are responsible for any sounds, timers, transitions, starting/pausing/stopping physics, etc.

In the ideal flow of the composer scene, there is an expectation that scene:create() gets called once.  After that you get a series of scene:show() and scene:hide() calls.  Each :show() brings a scene on screen, each :hide() moves it off screen.  Any display object in the scene’s view (i.e. sceneGroup), will be moved on and off each time a composer:gotoScene() is called.

If your scene needs to reset, you would move your objects back to their home position during the “will” phase of scene:show().  If you need to pause your game while you go to another scene, you will pause/stop those items Composer does not manage (audio, timers, transitions, physics) in the scene:hide()'s “will” phase as you want them to stop before the transition off screen and then you resume them in the scene’s scene:show()'s “did” phase.

Rob

@gainsempire

Regarding your question of unloading the scene game.lua –

If your game scene is really heavy (display objects wise), then you’re probably better off leaving it alone (rather than removing it). When you re-enter game.lua from the main menu, just reset object states to being a new game.

I think that if you put it in the composer-provided group (self.view, aliased to scenegroup) it does work, but I don’t think you can guarantee other things, e.g. removal of event listeners.

Make sure you add all display objects to the sceneGroup. When a scene is removed, all objects within sceneGroup will be removed. Add objects with the following code: sceneGroup:insert(object).

Scenes are not removed from memory unless you remove them with the code: composer.removeScene(scene). You can not remove the active scene. So, if game.lua is the active scene, composer.removeScene(game) will not work. A workaround is to set up a blank scene. Let’s say its game over and you want you have a button to play again. If you simply load game.lua ontop of itself, you will have two instances of the scene loaded on the screen. It won’t work. Instead, have the play again button point to a blank scene. Within the blank scene, add a function which handles the removal of the game scene and the reloading of it. Then, set up a timer to run the function. It would look something like this.

function reLoadGame()

     composer.removeScene(game)

     composer.gotoScene(game)

end 

timer.performWithDelay(1000, reLoadGame)

You will need to handle widget Button and event listener removal on your own, as they are not part of scene cleanup. This can be handled within the scene:destroy() function.

Composer only manages display objects.  You are responsible for any sounds, timers, transitions, starting/pausing/stopping physics, etc.

In the ideal flow of the composer scene, there is an expectation that scene:create() gets called once.  After that you get a series of scene:show() and scene:hide() calls.  Each :show() brings a scene on screen, each :hide() moves it off screen.  Any display object in the scene’s view (i.e. sceneGroup), will be moved on and off each time a composer:gotoScene() is called.

If your scene needs to reset, you would move your objects back to their home position during the “will” phase of scene:show().  If you need to pause your game while you go to another scene, you will pause/stop those items Composer does not manage (audio, timers, transitions, physics) in the scene:hide()'s “will” phase as you want them to stop before the transition off screen and then you resume them in the scene’s scene:show()'s “did” phase.

Rob

@gainsempire

Regarding your question of unloading the scene game.lua –

If your game scene is really heavy (display objects wise), then you’re probably better off leaving it alone (rather than removing it). When you re-enter game.lua from the main menu, just reset object states to being a new game.