Search code examples
luacoronasdk

Why doesn't corona composer let you go back to the main.lua page


When I'm using

composer.gotoscene("main")

An error pops up with the message:

"Attempt to concatenate global 'sceneName' stack traceback".

If I use another page to redirect instead of main.lua (example "scene2") then it works.


Solution

  • All of the code for the game should be in its own Composer scene rather than main.lua. If the Composer scene with the play button is called HomeScene.lua, in main.lua you would put

    local composer = require "composer"
    composer.gotoScene( "HomeScene" )
    

    This makes the scene with the play button load and appear first. If you want to initialize your game scene before the player can have a chance to press Play and start the game, you can add composer.loadScene("GameScene") in main.lua as well. This just creates the scene and stores it without showing it.

    In HomeScene.lua, if the play button is a DisplayObject called playButton, you would do something like

    local composer = require "composer"
    
    local function handleTouch( event )
        if event.phase == "began" then
            composer.gotoScene( "GameScene" )
        end
    end
    
    playButton:addEventListener( "touch", handleTouch )
    

    Hope this helps.