Hello everyone.
I am building a game using Storyboard that has 3 scenes (titleScene, selectLevelScene and playScene).
I can navigate between them without a problem tapping on the on-screen buttons.
I am trying to handle the Android hardware “back” and “menu” buttons to go back to the previous scene as the on-screen buttons do. When I try to do this i get this error and the screen turns black and accepts no other input:
Runtime error ?:0: attempt to index field 'contentBounds' (a nil value) main.lua:191: in function 'toScene' main.lua:215: in function '\_listener'
At line 191 there is a storyboard.gotoScene function call.
At line 215 is the function call from the onKeyEvent listener function that eventually calls the gotoScene function (code below).
I personally did not used the ‘contentBounds’ in this game. I am thinking maybe a build-in function uses is.
Here is the function that I use to navigate between scenes located in main.lua:
[lua]
function toScene(sceneName) storyboard.purgeScene(storyboard.getCurrentSceneName()) storyboard.gotoScene( sceneName, "crossFade", 600) -- this is line 191 end
[/lua]
I always make sure that sceneName parameter is not equal to storyboard.getCurrentSceneName().
Here is the code that I use to handle key events located in main.lua:
[lua]
local function onKeyEvent( event ) local returnValue = false local keyPhase = event.phase local keyName = event.keyName if storyboard.getCurrentSceneName() ~= nil and keyName ~= nil and keyPhase ~= nil then if "back" == keyName or "menu" == keyName then if "up" == keyPhase then returnValue = true if "titleScene" == storyboard.getCurrentSceneName() and "back" == keyName then local alert = native.showAlert( "Confirmation", "Are you sure you want to exit?", { "Yes", "No" }, onExitAlertAction ) elseif "selectLevelScene" == storyboard.getCurrentSceneName() then toScene("titleScene") -- this is line 215 elseif "playScene" == storyboard.getCurrentSceneName() then toScene("selectLevelScene") end end end end return returnValue end Runtime:addEventListener( "key", onKeyEvent )
[/lua]
The onKeyEvent function works great, in that I print()ed the key’s name, phase and the current scene the game finds itself successfully. The alert and exit works as well. Just the changing of the scene when I press the “back” or “menu” button (gotoScene()) doesn’t work.
I searched the Corona forums and on google for a few days to find solution but couldn’t.
I tried to add the runtime event listener in every scene on the enterScene() and remove it on the exitScene(), same error.
I tried to remove all the object event listeners on the exitScene() even though I heard that they will automatically be removed, same error.
There are a few others having this problem and I am not sure how they fixed it.
Here are a few links I read:
http://forums.coronalabs.com/index.php?app=core&module=search§ion=search&do=search&fromsearch=1
After I find an answer, from here or elsewhere I will post it here, maybe it would be of some help for someone.
Thank you.