1

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

这是我的代码:

主程序.lua

local storyboard = require "storyboard"
storyboard.gotoScene("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.

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

4

3 回答 3

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
于 2013-05-13T05:14:54.430 回答
0

首先检查你在listener,level1,about中编写的所有lua文件,它的名称应该与适当的大小写敏感相同。

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

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

喜欢:

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

--你的level1.lua的最后一行

return scene
于 2014-01-21T07:32:26.933 回答
0

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

于 2017-07-24T13:26:25.690 回答