场景和组使用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
在您的错误输出中,它要求一个unload me
函数。
尝试将以下代码添加到您的代码中(在return
语句之前),看看是否有所改变:
function unloadMe()
print( "test" )
end
看看它是否可以消除错误。另一种选择是弃用ui.lua
并使用新的小部件库的widget.newButton()
函数。以下是该函数的文档页面(语法与您已经拥有的几乎相同,因此应该不需要太多更改):
http://developer.anscamobile.com/reference/index/widgetnewbutton
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
我自己没有使用 director.lua,所以不完全确定,但是在你的 options.lua 文件中,你应该把以下两行代码放到 new() 函数中:
local assetPath = "assets/" local localGroup = display.newGroup()
然而在你的 titlescreen.lua 文件中,这些代码在 new() 函数上面,我认为应该是这样的。
一般来说,你应该让缩进保持一致,这样很容易看出哪些代码在哪些代码块内。