![](/img/trans.png)
[英]attempt to call method 'print' (a nil value) when implementation OOP in Lua
[英]Corona Runtime Error: “attempt to call a nil value”
下面的代碼來自一個在太空射擊游戲中創建並返回外星飛船到main.lua的類。
我需要包含一個函數來確定如果這艘船遇到一個物體會發生什么,但是當我運行代碼時,外星人的船確實遇到了什么,我得到了Corona運行時錯誤:
試圖調用零值 - 開始追溯:[C] :?
-- COLLISION FUNCTION
local function xenosColl(event)
if (event.phase == "began") then
print("hahf")
end
end
-- XENOS SHIP
function xenosShip.new()
local newXenosShip=display.newSprite( alShipSheet, alShipSeqData )
newXenosShip:play()
newXenosShip.x=580
newXenosShip.y=70
newXenosShipShape = {0,-40 , 60,0 , 0,40 , -60,0}
newXenosShip.myName = "newXenosShip"
physics.addBody(newXenosShip,"dynamic", {density = 1.0, friction = 0.3, bounce = 1, shape = newXenosShipShape})
newXenosShip:applyForce(0,2000,newXenosShip.x,newXenosShip.y)
newXenosShip:addEventListener("collision", xenosColl)
return setmetatable(newXenosShip, xenosShip_mt)
end
return xenosShip
如果我刪除碰撞eventlistener,沒有錯誤,外星船只碰到另一個對象,所以它試圖調用函數的方式肯定有問題,但我無法弄清楚是什么。
我就像你一樣創建相同的對象,當我添加return setmetatable(newXenosShip, xenosShip_mt)
時我得到錯誤我認為當你使用setmetatable時它會導致對象變為nil。 嘗試刪除metatable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.