Problem with scenes

Hello, can you help me with my game?

In game i have two levels and I created scenes.

Normal game:

main -> menu -> lvl1 -> tolvl2 -> lvl2

If you lose in lvl 1:

main -> menu -> lvl1 -> restart1 -> lvl1

If you lose in lvl 2:

main -> menu -> lvl1 -> tolvl2 -> lvl2 -> restart2

Example in restart1 is:

local composer = require( “composer” )
local scene = composer.newScene()
composer.removeScene( “lvl1” )
composer.removeHidden()

composer.gotoScene(“lvl1”)

return scene

In tolvl2 is:

local composer = require( “composer” )
local scene = composer.newScene()
composer.removeScene( “lvl1” )
composer.removeHidden()

composer.gotoScene(“lvl2”)

return scene

Problem is in: lvl2 -> restart2 -> lvl1

because, when i lose in lvl2 is displayed losing text from lvl1 and lvl2.

I think, lvl1 scene is not removed. When I’m trying to remove manual, objects from lvl1 is not exist.

What can I do for fix it?
Sorry for my bad english.

Wiktor

Can anybody help?

Hi,

I am not a Corona expert yet, but I’ll try to help.

Have you implemented scene:destroy()? 

When you call composer.removeScene( “lvl1” ), I think it fires a destroy event.

You need to have scene:addEventListener(“destroy”, scene) and a destroy method implemented.

Thanks

endy

wik, 

When using Composer, you should be using the scene template provided by Corona for all of your ‘scenes’. You should not change anything in that template, just add to it. Here is that template:

---------------------------------------------------------------------------------- -- -- scenetemplate.lua -- ---------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

Before going further, I’d recommend reading through some tutorials on how to use Composer. There are a lot of them out there; here are a few to read through:

http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

http://coronalabs.com/blog/2014/06/03/tutorial-understanding-the-composer-api/

http://docs.coronalabs.com/guide/system/composer/index.html

I don’t understand. When I use composer goto.scene (“scene1”) then in scene1: scene:create -> scene:show ?

Can someone explain to me step by step how this template is working?

Can anybody help?

Hi,

I am not a Corona expert yet, but I’ll try to help.

Have you implemented scene:destroy()? 

When you call composer.removeScene( “lvl1” ), I think it fires a destroy event.

You need to have scene:addEventListener(“destroy”, scene) and a destroy method implemented.

Thanks

endy

wik, 

When using Composer, you should be using the scene template provided by Corona for all of your ‘scenes’. You should not change anything in that template, just add to it. Here is that template:

---------------------------------------------------------------------------------- -- -- scenetemplate.lua -- ---------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() --------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

Before going further, I’d recommend reading through some tutorials on how to use Composer. There are a lot of them out there; here are a few to read through:

http://coronalabs.com/blog/2014/01/21/introducing-the-composer-api-plus-tutorial/

http://coronalabs.com/blog/2014/06/03/tutorial-understanding-the-composer-api/

http://docs.coronalabs.com/guide/system/composer/index.html

I don’t understand. When I use composer goto.scene (“scene1”) then in scene1: scene:create -> scene:show ?

Can someone explain to me step by step how this template is working?