Search code examples
androidsdkluacoronasdk

Corona SDK gotoScene error


So I'm making an app in Corona SDK and I'm getting an error when I click my button that is trying to load up my first level.

Here are my codes:

main.lua

local storyboard = require "storyboard"
storyboard.gotoScene("menu")

menu.lua

local storyboard = require ("storyboard")
local scene = storyboard.newScene()

function scene:createScene( event )

    local screenGroup = self.view

    -- Background
    bg = display.newImage("images/bg.png")
    bg.x = display.contentCenterX
    bg.y = display.contentCenterY
    screenGroup:insert(bg)

    -- Title
    title = display.newImage("images/title.png")
    title.x = display.contentCenterX
    title.y = display.contentCenterY - 100
    screenGroup:insert(title)

    -- Play game
    play = display.newImage("images/playgame.png")
    play.x = display.contentCenterX - 170
    play.y = display.contentCenterY - 27
    screenGroup:insert(play)

    -- About Us
    about = display.newImage("images/about.png")
    about.x = display.contentCenterX - 100
    about.y = display.contentCenterY + 40
    screenGroup:insert(about)

    -- Level Select
    select = display.newImage("images/select.png")
    select.x = display.contentCenterX
    select.y = display.contentCenterY + 100
    screenGroup:insert(select)

end

function start(event)
    if event.phase == "began" then
        storyboard.gotoScene("level1", "fade", 400)
    end
end

function start2(event)
    if event.phase == "began" then
        storyboard.gotoScene("about", "fade", 400)
    end
end

function start3(event)
    if event.phase == "began" then
        storyboard.gotoScene("selectlvl", "fade", 400)
    end
end

function scene:enterScene(event)

    play:addEventListener("touch", start)
    about:addEventListener("touch", start2)
    select:addEventListener("touch", start3)

end

scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )

return scene

And I have 0 code in my level1.lua

The error that I'm getting is this:

Runtime error
    ?:0: attempt to concatenate global 'sceneName' (a nil value)
stack traceback:
    [C]: ?
    ?: in function 'gotoScene'
    ...s\corona projects\stickman obsticale course\menu.lua.42: in function
<...s\corona projects\stickman obsticale course\menu.

Thanks for reading and I hope you can find an answer, because I can't :)


Solution

  • The problem is that you do have 0 code in your level1.lua. I'm going to send you this template code everyone uses when they first start using storyboard.

    The problem with your code is that when you went to level 1, there was no code to create a scene. Also, there was no code for when the scene entered, exited, or was destroyed. In this way, your code on menu.lua had no where to go. Copy paste the code below into level 1. When I ran your code with the scene template, it worked for me.

    On another note, I noticed your functions and display objects were created in createScene. I would put those in enterScene, because they should only be called once the page is loaded.

    Hope this helps! Contact me if your code doesn't work or if you have further problems.

    ----------------------------------------------------------------------------------
    
    local storyboard = require( "storyboard" )
    local scene = storyboard.newScene()
    
    ----------------------------------------------------------------------------------
    -- 
    --  NOTE:
    --  
    --  Code outside of listener functions (below) will only be executed once,
    --  unless storyboard.removeScene() is called.
    -- 
    ---------------------------------------------------------------------------------
    
    ---------------------------------------------------------------------------------
    -- BEGINNING OF YOUR IMPLEMENTATION
    ---------------------------------------------------------------------------------
    
    -- Called when the scene's view does not exist:
    function scene:createScene( event )
        local group = self.view
    
        -----------------------------------------------------------------------------
    
        --  CREATE display objects and add them to 'group' here.
        --  Example use-case: Restore 'group' from previously saved state.
    
        -----------------------------------------------------------------------------
    
    end
    
    
    -- Called immediately after scene has moved onscreen:
    function scene:enterScene( event )
        local group = self.view
    
        print("entered")
    
        -----------------------------------------------------------------------------
    
        --  INSERT code here (e.g. start timers, load audio, start listeners, etc.)
    
        -----------------------------------------------------------------------------
    
    end
    
    
    -- Called when scene is about to move offscreen:
    function scene:exitScene( event )
        local group = self.view
    
        -----------------------------------------------------------------------------
    
        --  INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.)
    
        -----------------------------------------------------------------------------
    
    end
    
    
    -- Called prior to the removal of scene's "view" (display group)
    function scene:destroyScene( event )
        local group = self.view
    
        -----------------------------------------------------------------------------
    
        --  INSERT code here (e.g. remove listeners, widgets, save state, etc.)
    
        -----------------------------------------------------------------------------
    
    end
    
    ---------------------------------------------------------------------------------
    -- END OF YOUR IMPLEMENTATION
    ---------------------------------------------------------------------------------
    
    -- "createScene" event is dispatched if scene's view does not exist
    scene:addEventListener( "createScene", scene )
    
    -- "enterScene" event is dispatched whenever scene transition has finished
    scene:addEventListener( "enterScene", scene )
    
    -- "exitScene" event is dispatched before next scene's transition begins
    scene:addEventListener( "exitScene", scene )
    
    -- "destroyScene" event is dispatched before view is unloaded, which can be
    -- automatically unloaded in low memory situations, or explicitly via a call to
    -- storyboard.purgeScene() or storyboard.removeScene().
    scene:addEventListener( "destroyScene", scene )
    
    ---------------------------------------------------------------------------------
    
    return scene