I’m having an issue with Composer. I have 1st scene I call Menu.lua and then 2nd scene Levels.lua and 3rd scene level01.lua where I have the level I want to play. I use print command to see in output window what is happening and this is the output
Menu:create
Menu:show will
Menu:show did
Menu:hide will
levels01: create
levels01: hide will
levels01: hide did
levels01: show will
levels01: show did
Menu:destroy
levels01: show will
levels01: show did
Levels enter first “Hide” and then “Show” and then “Show” again. If I run levels01 straight from Main scene then the output is correct:
levels01: create
levels01: show will
levels01: show did
this behaviour is causing a lot of issues with event listeners and functions. Problem is that if scene enters first to “hide” where I’m removing event listener instead of entering “show” where I’m adding event listener I end up with nil error. That makes sense because I’m trying to remove event listener which does not exist yet. How to fix composer to enter scene in the right order?
print( composer.getSceneName("current") .. " - [here your scene name]: " .. event.name )
You will see when you call next scene, the current scene is level1 and event is hide, but the event is related to menu scene
To fix my problem I created a sceneLoaded = false variable at the scene, when the scene calls did phase on show event I put sceneLoaded = true. Then I check in my next & back button listener if sceneLoaded = true before call gotoScene
I don’t know if there are a better way, but works for me.
print( composer.getSceneName("current") .. " - [here your scene name]: " .. event.name )
You will see when you call next scene, the current scene is level1 and event is hide, but the event is related to menu scene
To fix my problem I created a sceneLoaded = false variable at the scene, when the scene calls did phase on show event I put sceneLoaded = true. Then I check in my next & back button listener if sceneLoaded = true before call gotoScene
I don’t know if there are a better way, but works for me.
I think the issue is that Runtime listeners just run the whole time so what is probably happening is that Runtime listener from Scene1 is still running in Scene2. This is causing wrong behaviour. It’s just my guess.
@koormo I think your solution is quite good. In my case I renamed the names of functions
function scene:show( event ) local sceneGroup = self.view if phase == "will" then -- do something elseif phase == "did" then -- do something end end scene:addEventListener( "show", scene )
and for another scene I’m using
function scene2:show( event ) local sceneGroup = self.view if phase == "will" then -- do something elseif phase == "did" then -- do something end end scene2:addEventListener( "show", scene2 )
I think the issue is that Runtime listeners just run the whole time so what is probably happening is that Runtime listener from Scene1 is still running in Scene2. This is causing wrong behaviour. It’s just my guess.
@koormo I think your solution is quite good. In my case I renamed the names of functions
function scene:show( event ) local sceneGroup = self.view if phase == "will" then -- do something elseif phase == "did" then -- do something end end scene:addEventListener( "show", scene )
and for another scene I’m using
function scene2:show( event ) local sceneGroup = self.view if phase == "will" then -- do something elseif phase == "did" then -- do something end end scene2:addEventListener( "show", scene2 )