【问题标题】:Scenes and groups trouble using Lua and Corona SDK使用 Lua 和 Corona SDK 的场景和组遇到问题
【发布时间】:2017-12-31 11:32:23
【问题描述】:

我的按钮和场景更改有问题,我的标题屏幕场景中的按钮既可以工作又可以直接到相应的屏幕,但它们只会执行一次。所以我无法导航到选项,然后返回标题屏幕,然后再次返回选项 - 我不知道为什么?

这是我的标题屏幕文件:

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”中 ?: 在函数中 ?: 在函数中

【问题讨论】:

    标签: lua navigation coronasdk


    【解决方案1】:

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

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

    但是在您的 titlescreen.lua 中,这些行在 new() 函数之上,我认为这就是它需要的样子。

    一般来说,你应该让你的缩进保持一致,这样就很容易注意到哪些代码在哪些代码块中。

    【讨论】:

      【解决方案2】:

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

      尝试将其添加到您的代码中(在 return 语句上方),看看它是否会有所作为:

      function unloadMe()
          print( "test" )
      end
      

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-17
        • 2016-04-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-11
        • 2014-01-28
        相关资源
        最近更新 更多