Lua碰撞零值
当我试图执行以下代码时,它给出了以下错误: 尝试索引字段“其他”(零值) 但我不知道为什么 守则:Lua碰撞零值,lua,physics,collision,null,Lua,Physics,Collision,Null,当我试图执行以下代码时,它给出了以下错误: 尝试索引字段“其他”(零值) 但我不知道为什么 守则: function onCollision(event) if event.phase == "began" then if event.other.star == "star" then score = score + 1 elseif event.other.mine1 == "mine1" then if jet.collided == fals
function onCollision(event)
if event.phase == "began" then
if event.other.star == "star" then
score = score + 1
elseif event.other.mine1 == "mine1" then
if jet.collided == false then
timer.cancel(tmr)
jet.collided = true
jet.bodyType = "static"
explode()
end
end
end
end
提前感谢:)正如@lhf和@RBerteig所说的问题是
事件。其他是nil
,因此尝试访问star
成员无法尝试索引nil值
假设event.other
确实可以是nil
,解决问题的惯用方法是在前面的ifif event.phase==“start”和event.other then
,因为if和else条件都取决于要设置的event.other
function onCollision(event)
if event.phase == "began" and event.other then
if event.other.star == "star" then
score = score + 1
elseif event.other.mine1 == "mine1" then
if jet.collided == false then
timer.cancel(tmr)
jet.collided = true
jet.bodyType = "static"
explode()
end
end
end
end
如果您想知道“尝试索引字段”的消息,您也可以阅读更多关于的信息,因为@lhf和@RBerteig说问题是事件。其他是nil
,因此尝试访问star
成员无法尝试索引nil值
假设event.other
确实可以是nil
,解决问题的惯用方法是在前面的ifif event.phase==“start”和event.other then
,因为if和else条件都取决于要设置的event.other
function onCollision(event)
if event.phase == "began" and event.other then
if event.other.star == "star" then
score = score + 1
elseif event.other.mine1 == "mine1" then
if jet.collided == false then
timer.cancel(tmr)
jet.collided = true
jet.bodyType = "static"
explode()
end
end
end
end
如果您想知道“尝试索引字段”的消息,您也可以阅读更多关于的信息,这意味着事件。其他为零。lhf所说的。这意味着传递给onCollision
的值是一个表(或类似于表的庸医),但它没有名为other
的字段。也许该字段实际上被命名为Other
。也许onCollision
没有传递您认为它已经传递的对象。这意味着event.other
为零。lhf所说的。这意味着传递给onCollision
的值是一个表(或类似于表的庸医),但它没有名为other
的字段。也许该字段实际上被命名为Other
。可能onCollision
没有传递您认为它已经传递的对象。