场景和组在使用Lua和Corona SDK时遇到问题
我的按钮和场景更改都被窃听了,我的标题屏幕场景上的按钮可以工作并直接指向相应的屏幕,但它们只能执行一次。因此,我无法导航到选项,然后返回标题屏幕,然后再次返回选项-我无法找出原因 这是我的标题屏幕文件:场景和组在使用Lua和Corona SDK时遇到问题,lua,navigation,coronasdk,Lua,Navigation,Coronasdk,我的按钮和场景更改都被窃听了,我的标题屏幕场景上的按钮可以工作并直接指向相应的屏幕,但它们只能执行一次。因此,我无法导航到选项,然后返回标题屏幕,然后再次返回选项-我无法找出原因 这是我的标题屏幕文件: module(..., package.seeall) local assetPath = "assets/" local mainGroup = display.newGroup() function new() local ui = require("ui") lo
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/options屏幕。lua:30:错误:应为表。如果这是一个函数调用,则可能使用了“.”而不是“:”
堆栈回溯:
[C] :?
[C] :在函数“插入”中
/Users/Lewis/Desktop/proj/optionsScreen.lua:30:功能“新建”
/Users/Lewis/Desktop/proj/director.lua:118:in函数“loadScene”
/Users/Lewis/Desktop/proj/director.lua:415:函数中的“changeScene”
/Users/Lewis/Desktop/proj/titlescreen.lua:67:在函数“OneEvent”中
/Users/Lewis/Desktop/proj/ui.lua:94:在函数中
?:在功能上
运行时错误
/Users/Lewis/Desktop/proj/director.lua:151:尝试调用字段“unloadMe”(一个空值)
堆栈回溯:
[C] :在“卸载”功能中
/Users/Lewis/Desktop/proj/director.lua:151:in function'\u listener'
?:在功能上
?:在函数中我自己不使用director.lua,所以我不是100%确定,但在您的options.lua中,您在new()函数中放了以下两行:
local assetPath = "assets/"
local localGroup = display.newGroup()
但是,在titlescreen.lua中,这些行在new()函数的上方,我认为这就是它需要的
一般来说,您应该使缩进保持一致,以便很容易注意到哪些代码在哪些代码块内。在错误打印输出中,它要求使用卸载我函数 尝试将其添加到您的代码中(在return语句上方),看看是否会有所不同:
function unloadMe()
print( "test" )
end
看看这是否能消除错误。另一个选择是抛弃ui.lua,改用新的小部件库的widget.newButton()函数。下面是文档页面(语法与您已有的几乎相同,因此不需要太多更改):