Corona SDK gotoScene错误

时间:2013-05-12 01:19:04

标签: android sdk lua corona

所以我在Corona SDK中制作应用程序,当我点击试图加载我的第一级别的按钮时出现错误。

以下是我的代码:

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

我的level1.lua

中有0个代码

我得到的错误是:

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.

感谢您的阅读,我希望您能找到答案,因为我不能:)

3 个答案:

答案 0 :(得分:1)

问题是你的level1.lua中有0个代码。我将向您发送每个人在第一次开始使用故事板时使用的模板代码。

代码的问题在于,当你进入第1级时,没有代码来创建场景。此外,没有代码用于何时进入,退出或销毁场景。通过这种方式,menu.lua上的代码无处可去。将以下代码复制粘贴到级别1.当我使用场景模板运行代码时,它对我有用。

另一方面,我注意到你的函数和显示对象是在createScene中创建的。我会将它们放在enterScene中,因为它们只应在页面加载后调用。

希望这有帮助!如果您的代码不起作用或者您还有其他问题,请与我联系。

----------------------------------------------------------------------------------

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

答案 1 :(得分:0)

首先检查你在监听器,level1,about中写的所有lua文件,它的名称应该与正确的区分大小写相同。

你的level1.lua会有问题,不应该代码。

如果你想用带有0代码的storyboard实现它。请确保您已在顶部实现了storyboard对象,并且其场景从底部正确返回。

喜欢:

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

- level1.lua的最后一行

return scene

答案 2 :(得分:0)

马修是对的。跟着他的屁股。 我只是想添加一个您可能会发现有用的未来提示。当调试错误代码时,如果代码声明全局(一个零值),则表示某些东西是空的,如果没有任何东西,则无法调用该对象。和? in function表示包含空值的特定对象或函数。如果再次看到此错误,请检查拼写错误。