简体   繁体   English

电晕运行时错误:“尝试调用零值”

[英]Corona Runtime Error: “attempt to call a nil value”

The code below is from a class that creates and returns an alien ship to main.lua in a space shooter game. 下面的代码来自一个在太空射击游戏中创建并返回外星飞船到main.lua的类。

I need to include a function that determines what happens if this ship hits an object, but when I run the code, and the alien ship does hit something, I get the Corona Runtime Error: 我需要包含一个函数来确定如果这艘船遇到一个物体会发生什么,但是当我运行代码时,外星人的船确实遇到了什么,我得到了Corona运行时错误:

attempt to call a nil value - start traceback: [C]:? 试图调用零值 - 开始追溯:[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

If I remove the collision eventlistener, there is no error and the alien ship just bumps into the other object, so there must be something wrong with how it attempts to call the function, but I can't figure out what. 如果我删除碰撞eventlistener,没有错误,外星船只碰到另一个对象,所以它试图调用函数的方式肯定有问题,但我无法弄清楚是什么。

i create the same object just like you did and i get the error when i add the return setmetatable(newXenosShip, xenosShip_mt) i think it causes the object to become nil when you use setmetatable. 我就像你一样创建相同的对象,当我添加return setmetatable(newXenosShip, xenosShip_mt)时我得到错误我认为当你使用setmetatable时它会导致对象变为nil。 try to remove the metatable 尝试删除metatable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM