场景和组使用Lua和Corona SDK时出现问题

我的按钮和场景更改有问题,我标题屏幕下方的按钮都有效,并且可以直接导向适当的屏幕,但它们只能执行一次。因此,我无法导航到选项,然后返回到标题屏幕,然后再返回到选项 - 我无法找出原因在哪里?

这是我的titlescreen文件:

module(..., package.seeall)

local assetPath = "assets/"

local mainGroup = display.newGroup()

function new()

    local ui = require("ui")
    local titleScreen = display.newImageRect(assetPath .. "mainMenu.png", display.contentWidth, display.contentHeight)
    titleScreen.x = display.contentWidth / 2
    titleScreen.y = display.contentHeight / 2
    mainGroup:insert(titleScreen)

        local onPlayTouch = function( event )
            if event.phase == "release" then
            director:changeScene("gameScreen")
            end
        end

        local playButton = ui.newButton{
        defaultSrc = assetPath .. "playnowbtn.png",
        defaultX = 222,
        defaultY = 62,
        overSrc = assetPath .. "playnowbtn-over.png",
        overX = 222,
        overY = 62,
        onEvent = onPlayTouch
        }

        playButton.x = display.contentWidth / 2
        playButton.y = 50;
        mainGroup:insert( playButton )

        local onOptionTouch = function( event )
            if event.phase == "release" then
            director:changeScene("optionsScreen")
            end
        end

        local optionButton = ui.newButton{
        defaultSrc = assetPath .. "playnowbtn.png",
        defaultX = 222,
        defaultY = 62,
        overSrc = assetPath .. "playnowbtn-over.png",
        overX = 222,
        overY = 62,
        onEvent = onOptionTouch
        }

        optionButton.x = display.contentWidth / 2
        optionButton.y = 190;
        mainGroup:insert( optionButton )

    return mainGroup
end

我的选项文件如下:

module(..., package.seeall)

function new()

local assetPath = "assets/"

local localGroup = display.newGroup()

local background = display.newImage (assetPath .."optionsScreen.png")
localGroup:insert(background)

    local onBackTouch = function( event )
        if event.phase == "release" then
        director:changeScene("titleScreen")
        end
    end

    local backButton = ui.newButton{
    defaultSrc = assetPath .. "playnowbtn.png",
    defaultX = 222,
    defaultY = 62,
    overSrc = assetPath .. "playnowbtn-over.png",
    overX = 222,
    overY = 62,
    onEvent = onBackTouch
    }

    backButton.x = display.contentWidth / 2
    backButton.y = display.contentHeight / 2
    localGroup:insert(backbutton)

    return localGroup
end

现在,该按钮显示在选项场景上并响应触摸,但无法直接返回到标题屏幕。

我认为我对组混淆了,只将图像分配给场景而不是整个游戏?

有谁能帮我解决问题,谢谢。

编辑:

当单击按钮时,我还收到以下运行时错误。

运行时错误 /Users/Lewis/Desktop/proj/optionsScreen.lua:30: 错误:需要表格。如果这是函数调用,则可能使用“。”而不是“:” 堆栈回溯: [C]:? [C]:在函数“插入”中 /Users/Lewis/Desktop/proj/optionsScreen.lua:30: 在函数“新建”中 /Users/Lewis/Desktop/proj/director.lua:118: 在函数“loadScene”中 /Users/Lewis/Desktop/proj/director.lua:415: 在函数“changeScene”中 /Users/Lewis/Desktop/proj/titlescreen.lua:67: 在函数“onEvent”中 /Users/Lewis/Desktop/proj/ui.lua:94: 在函数中 ?:在函数中 运行时错误 /Users/Lewis/Desktop/proj/director.lua:151: 尝试调用字段“unloadMe”(空值) 堆栈回溯: [C]:在函数“unloadMe”中 /Users/Lewis/Desktop/proj/director.lua:151: 在函数“_listener”中 ?:在函数中 ?:在函数中

原文链接 https://stackoverflow.com/questions/6380214

点赞
stackoverflow用户686008
stackoverflow用户686008

我自己没有使用 director.lua,所以不完全确定,但是在你的 options.lua 文件中,你应该把以下两行代码放到 new() 函数中:

local assetPath = "assets/"
local localGroup = display.newGroup()

然而在你的 titlescreen.lua 文件中,这些代码在 new() 函数上面,我认为应该是这样的。

一般来说,你应该让缩进保持一致,这样很容易看出哪些代码在哪些代码块内。

2011-06-20 05:15:54
stackoverflow用户731940
stackoverflow用户731940

在您的错误输出中,它要求一个unload me函数。

尝试将以下代码添加到您的代码中(在return语句之前),看看是否有所改变:

function unloadMe()
    print( "test" )
end

看看它是否可以消除错误。另一种选择是弃用ui.lua并使用新的小部件库的widget.newButton()函数。以下是该函数的文档页面(语法与您已经拥有的几乎相同,因此应该不需要太多更改):

http://developer.anscamobile.com/reference/index/widgetnewbutton

2011-07-26 17:44:50