下面的代码来自一个在太空射击游戏中创建外星飞船并将其返回到 main.lua 的类。
我需要包含一个函数来确定如果这艘船撞到一个物体会发生什么,但是当我运行代码时,外星飞船确实撞到了东西,我得到了 Corona 运行时错误:
尝试调用 nil 值 - 开始回溯:[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
如果我删除碰撞事件监听器,没有错误,外星飞船只是撞到另一个物体,所以它尝试调用函数的方式一定有问题,但我不知道是什么。