您好我正在将这个简单的“抓鸡蛋”游戏从Godot引擎转换为Corona。我是编程新手,并且正在使用这个项目作为学习练习。
但是,我遇到了障碍。我一直收到以下错误信息:**
错误:运行时错误 C:\ Users \ kdoug \ Documents \ Corona Projects \ cathchtheegg \ main.lua:19:尝试将数字与nil进行比较 堆栈追溯: C:\ Users \ kdoug \ Documents \ Corona Projects \ cathchtheegg \ main.lua:19: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 = {}
---------BACKGROUND---------------
local bg = dnir("bg.png", w,h)
bg.x = cx
bg.y = cy
----------DISPLAY BASKET------------
bask = dnir("basket.png", 100,50)
bask.x = cx
bask.y = cy
physics.addBody(bask,"kinematic")
bask.myName = "bask"
----- BASKET MOVE W/ MUSE FUNCTION -----
local function baskMove (e)
bask.x = e.x
bask.y = e.y
end
Runtime:addEventListener("mouse", baskMove)
----------------GROUND---------------
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)
----------****DELETE EGG FUNCTION****------------
--function loop ()
-- if egg and egg.y > 100 then
-- print("Delete")
-- display.remove(egg)
-- end
--end
--
--Runtime:addEventListener("enterFrame", loop)
-----------COLLISIONS FUNCTIION-------------
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
--
--------------EGG---------------------
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 COLLISIION CB-------------
egg.collision = collision
egg:addEventListener("collision", egg)
end
--
-----------Spawn EGG-----------
function spawner()
theEgg()
print(#eggs)-- PRINT AMT IN TABLE
end
timer.performWithDelay(2000, spawner, 0)
答案 0 :(得分:0)
我不知道你何时删除了enterFrame监听器。这很重要。删除egg对象循环后可能会再次调用。因此,当egg.y未定义(= nil)时,无法进行比较(如果是法则)。
我的解决方案:
function loop ()
if egg and egg.y > 100 then
print("Delete")
display.remove(egg)
end
end
来自https://www.lua.org/pil/3.3.html
的信息所有逻辑运算符都将false和nil视为false和任何内容 否则为真
或(使用局部变量索引而不是全局变量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