簡體   English   中英

Corona SDK嘗試將數字與nil比較

[英]Corona SDK Attempt to compare number number to nil

嗨,我正在將這個簡單的“抓蛋”游戲從Godot引擎轉換為Corona。 我對編程非常陌生,並將這個項目用作學習練習。

但是我遇到了一個障礙。 我不斷收到以下錯誤消息:

**

錯誤:運行時錯誤C:\\ Users \\ kdoug \\ Documents \\ Corona Projects \\ cathchtheegg \\ main.lua:19:嘗試比較編號為nil的堆棧回溯:C:\\ Users \\ kdoug \\ Documents \\ Corona Projects \\ cathchtheegg \\ main.lua :19:在功能中?:在功能中

**

我要做的是看雞蛋在超過某個點時是否會刪除,而不必與物理對象發生碰撞。

任何幫助,將不勝感激! 謝謝

這是代碼(少了幾分):

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)

我不知道何時刪除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,並將其他所有內容視為true

或者(使用局部變量索引而不是全局變量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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM