Composer enters scene in wrong order

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?

 

This is link to my Dropbox

 

https://www.dropbox.com/sh/02392n8v0cdyrxn/AACfrWP9iq2BKTSQPx–wfjFa/Composer

i only see ‘levels01’ in your log. Are you sure you are calling ‘Levels.lua’ ?

If you are doing fast-transition through the pages, try using a ‘delay’ before calling a new page.

That was happening also to me.

The wrong order probably is that you are using something like that to check scenes & events

print( composer.getSceneName("current") .. event.name )

Try to change to this

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 only see ‘levels01’ in your log. Are you sure you are calling ‘Levels.lua’ ?

If you are doing fast-transition through the pages, try using a ‘delay’ before calling a new page.

That was happening also to me.

The wrong order probably is that you are using something like that to check scenes & events

print( composer.getSceneName("current") .. event.name )

Try to change to this

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 :slight_smile: 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 :slight_smile: 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 )