Corona SDK 尝试将数字与空值进行比较
2016-10-25 16:27:2
收藏:0
阅读:143
评论:1
嗨,我正在将这个简单的“接住鸡蛋”游戏从 Godot 引擎转换到 Corona。我很新于编程,并将此项目用作学习练习。
然而我遇到了一些问题,一直收到以下错误信息:
**
ERROR: Runtime error C:\Users\kdoug\Documents\Corona Projects\cathchtheegg\main.lua:19: attempt to compare number with nil stack traceback: C:\Users\kdoug\Documents\Corona Projects\cathchtheegg\main.lua:19: in function ?: in function
**
我想要做的是,查看鸡蛋是否超过了某个点会删除它,而不必使用物理对象的碰撞。
任何帮助都将不胜感激! 谢谢
以下是代码(稍微整理一下):
local physics = require "physics"
physics.start()
local h = display.actualContentHeight
local w = display.actualContentWidth
local cx = display.contentCenterX
local cy = display.contentCenterY
local dnir = display.newImageRect
local dnr = display.newRect
local mr = math.random
--local egg
local bask
local idx = 0
local eggs = {}
---------背景---------------
local bg = dnir("bg.png", w,h)
bg.x = cx
bg.y = cy
----------显示篮子------------
bask = dnir("basket.png", 100,50)
bask.x = cx
bask.y = cy
physics.addBody(bask,"kinematic")
bask.myName = "bask"
----- 篮子随鼠标移动的函数 -----
local function baskMove (e)
bask.x = e.x
bask.y = e.y
end
Runtime:addEventListener("mouse", baskMove)
----------------地面---------------
local grd = dnr(cx,h-470,w+50,10)
grd:setFillColor(.1, .8, .15,0)
grd.myName = "ground"
physics.addBody(grd, "static")
grd.collision = collision
grd:addEventListener("collision", grd)
----------****删除鸡蛋函数****------------
--function loop ()
-- if egg and egg.y > 100 then
-- print("Delete")
-- display.remove(egg)
-- end
--end
--
--Runtime:addEventListener("enterFrame", loop)
-----------碰撞函数-------------
local function collision ( s, e )
if e.phase == "began" then
if e.target.myName == "bask"
and e.other.myName == "egg" then
display.remove(e.other)
table.remove(eggs, idx)
end
if e.target.myName == "egg"
and e.other.myName == "bask" then
display.remove(e.target)
table.remove(eggs, idx)
end
if e.target.myName == "ground"
and e.other.myName == "egg" then
display.remove(e.other)
table.remove(eggs, idx)
end
if e.target.myName == "egg"
and e.other.myName == "ground" then
display.remove(e.target)
table.remove(eggs, idx)
end
end
end
--
--------------鸡蛋---------------------
function theEgg ()
egg = dnir("egg.png", 50,50)
physics.addBody(egg,"dynamic")
egg.myName = "egg"
idx = idx + 1
egg.x = mr(w)
egg.y = - 100
transition.to (egg, {y = h + 50, time= mr(1000,8000)})
eggs[idx] = egg
eggs[idx].idx = idx
print(eggs[idx])
--------鸡蛋碰撞回调-------------
egg.collision = collision
egg:addEventListener("collision", egg)
end
--
-----------生成鸡蛋-----------
function spawner()
theEgg()
print(#eggs)-- 打印表格数量
end
timer.performWithDelay(2000, spawner, 0)
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 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 代码?

我不知道您何时删除 enterFrame 监听器。这很重要。在您删除蛋对象之后,循环可能会再次调用。因此,当 egg.y 未定义 (等于 nil) 时,if 语句中的比较无法进行。
我的解决方案:
function loop() if egg and egg.y > 100 then print("Delete") display.remove(egg) end end来自 https://www.lua.org/pil/3.3.html 的信息:
或者(使用局部变量 index 替代全局变量 egg)。我不清楚您在代码中使用变量 egg 的目的,因此可能是错误的。
local index ... function loop() if eggs[index] and eggs[index].y > 100 then print("Delete") local egg = table.remove(eggs, index) display.remove(egg) egg = nil end end