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

问题描述 投票:0回答:2

我的按钮和场景变化都在烦恼,我的标题屏幕上的按钮既可以工作也可以直接指向相应的屏幕,但它们只会执行一次。所以我不能导航到选项,然后回到标题屏幕,然后再回到选项 - 我无法解决为什么?

这是我的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]:在函数'insert'/Users/Lewis/Desktop/proj/optionsScreen.lua:30:在函数'new'/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:in function?:in function运行时错误/Users/Lewis/Desktop/proj/director.lua:151:尝试调用字段'unloadMe'(零值)堆栈追溯:[C]:在函数'unloadMe '/Users/Lewis/Desktop/proj/director.lua:151:在函数'_listener'中?:在函数中?:在函数中

lua navigation corona
2个回答
0
投票

我自己不使用director.lua,所以我不是100%肯定,但是在你的options.lua中你将以下两行放在new()函数中:

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

但是在titlescreen.lua中,这些行位于new()函数之上,我认为这就是它需要的方式。

通常,您应该使缩进保持一致,以便很容易注意哪些代码在哪些代码块中。


0
投票

在您的错误打印输出中,它要求卸载我的功能。

尝试将此添加到您的代码中(在return语句之上),看看它是否有所不同:

function unloadMe()
    print( "test" )
end

并看看是否摆脱了错误。另一个选择是抛弃ui.lua并使用新的小部件库的widget.newButton()函数。这是该文档的页面(语法与您已有的几乎相同,因此不需要进行太多更改):

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

© www.soinside.com 2019 - 2024. All rights reserved.