我有一个创建宇宙飞船的类,我希望它包含一个函数,如果它撞到特定的墙壁,它会使飞船自行移除。但是,运行下面的代码,我收到以下错误:
...xenosShip.lua:16:
attempt to index global 'self' (a nil value)
stack traceback:
[C]:?
...xenosShip.lua:16: in function ...xenosShip.lua:14> ?:in function <?:218
>
我错过了什么?
local xenosShip = {}
-- XENOS SHIP COLLISION
local function xenosColl(event)
if (event.phase == "began" and event.other.myName == "bottomWall") then
self:removeSelf()
end
end
-- XENOS SHIP
function xenosShip.new()
local newXenosShip=display.newSprite( alShipSheet, alShipSeqData )
newXenosShip:play()
newXenosShip.x=300
newXenosShip.y=70
newXenosShip.myName = "newXenosShip"
physics.addBody(newXenosShip,"dynamic", {density = 1.0, friction = 0.3, bounce = 1})
newXenosShip:applyForce(50,2500,newXenosShip.x,newXenosShip.y)
newXenosShip:addEventListener("collision", xenosColl)
end
return xenosShip