Corona SDK 尝试将数字与空值进行比较

嗨,我正在将这个简单的“接住鸡蛋”游戏从 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)
点赞
用户7026995
用户7026995

我不知道您何时删除 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 的信息:

所有逻辑运算符都将 false 和 nil 视为 false,将其他任何值视为 true

或者(使用局部变量 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
2016-10-25 13:01:50