尝试在 'y' 字段上执行算术运算(一个空值)
2018-2-4 18:3:47
收藏:0
阅读:84
评论:2
我是新手,在 Corona 中遇到了这个错误,当游戏结束(lives=0)并且我试图移除背景(使用函数“move”移动背景)时出错:
“尝试在 'y' 字段上执行算术运算(一个空值)” 在第“background.y = background.y + 4”行
有人可以解释一下这是什么错误吗?
代码:
--添加物理
local physics = require("physics")
physics.start()
physics.setGravity(0,0)
local lives = 1
local died = false
--###
--添加背景
background = display.newImageRect("background.png",800,1400)
background.x = display.contentCenterX
background.y = 730
background.myName = "background"
--添加瓶子
bottiglia = display.newImageRect("bottiglia.png",41,104)
physics.addBody(bottiglia,"dynamic",{radius=45,bounce=0.5})
bottiglia.x = display.contentCenterX
bottiglia.y = 10
bottiglia.myName = "bottiglia"
--移动函数
local function move()
bottiglia.y = bottiglia.y + 4
background.y = background.y + 4
end
Runtime:addEventListener("enterFrame",move)
--###
--添加玩家
studente = display.newImageRect("studente.png",98,79)
studente.x = display.contentCenterX
studente.y = display.contentHeight - 100
physics.addBody(studente,{radius=40,isSensor=true})
studente.myName = "studente"
--###
--碰撞函数
local function onCollision(event)
if(event.phase == "began") then
local obj1 = event.object1
local obj2 = event.object2
if((obj1.myName == "studente" and obj2.myName == "bottiglia") or
(obj1.myName == "bottiglia" and obj2.myName == "studente")) then
if(died == false) then
died = true
--更新生命值
lives = lives - 1
livesText.text = "生命值: " .. lives
if(lives == 0) then
display.remove(studente)
display.remove(background)
timer.performWithDelay(100,endGame)
end
else
studente.alpha = 0
timer.performWithDelay(500,restoreStudente)
end
end
end
end
Runtime:addEventListener("collision",onCollision)
livesText = display.newText("生命值: " .. lives,200,80,native.systemFont,36)
--感谢大家
点赞
用户9369297
如果你不想移除监听器,你可以添加 nil 检查:
-- 移动函数
local function move()
if (bottiglia ~= nil and bottiglia.y ~= nil) then
bottiglia.y = bottiglia.y + 4
end
if (background ~= nil and background.y ~= nil) then
background.y = background.y + 4
end
end
使用全局变量 bottiglia 和 background 是相当危险的。
如果将它们设置为如下,就更加安全:
myGlobalsVars = { }
myGlobalsVars.myGlobalsVars = display.newGroup()
myGlobalsVars.background = display.newGroup()
2018-03-02 08:25:02
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在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 代码?

Runtime监听器(move函数)一直在工作。它改变了bottiglia和background对象的位置,但由于background已经不存在了,因此您会收到一个错误。一个简单的解决方案是在删除
background对象之前使用 Runtime:[removeEventListener()](https://docs.coronalabs.com/api/type/EventDispatcher/removeEventListener.html)移除全局监听器。使用
Runtime:removeEventListener(“enterFrame”,move)。