尝试调用全局变量'gameover'(一个表值)

我正在使用onCollision事件制作一个小游戏:

local function onCollision(event)
    if event.phase == "began" and gameIsActive == true then
        local obj1 = event.object1;
        local obj2 = event.object2;

        if obj1.name == "bill" then
        if     obj2.name == "rocks" then gameover()
        elseif obj2.name == ""   then
        end
    end
    end
end
Runtime:addEventListener( "collision", onCollision )

但是我有一个问题,代码在第一次运行时按预期工作,但是如果重新启动游戏,我会收到以下错误:

File: game.lua
Line: 649
Attempt to call global 'gameover' (a table value)

stack traceback: game.lua:649: in function <game.lua:643> ?: in function <?:221>

649 = if obj2.name == "rocks" then gameover()
643 = local function onCollision(event)

你有什么想法吗?

gameover函数:

function gameover()

rightTAP:removeEventListener("tap", movePLAYERr)
leftTAP:removeEventListener("tap", movePLAYERl)

timer.pause(spawnBits)
timer.pause(Rockspawner1)
timer.pause(tmrscore)
timer.pause(updateScoretimer)
timer.pause(spawnDinosControll)

audio.pause( drillingChannel )

drillbg.isVisible = false
scoreText.isVisible = false

Restartg = display.newText( "Restart", 0, 0,nil, 20)
Restartg:setFillColor(0, 0, 0)
screenGroup:insert(Restartg)
Restartg.x= display.contentWidth/2
Restartg.y= display.contentHeight/2 + 160

Restartg:addEventListener("tap", RestartGame1)

end
点赞
用户1442917
用户1442917

错误信息很明确:您正在尝试调用 gameover,但它具有表值而不是函数值。如果运行以下命令,则会收到相同的错误:gameover = {}; gameover()

这意味着要么您没有正确定义 gameover 函数,要么在脚本中某处覆盖了 gameover 值。

2014-10-15 18:19:08
用户4122321
用户4122321

如果您有一个包含函数对象的对象,则可以使用用于声明的键调用它们。

例如,

el = {}

print(el)
table: 0x7f8fe9e003f0

el.f1 = function()
print('hola')
end

el.f1()
hola
2014-10-15 20:40:33
用户1993254
用户1993254

将下面翻译成中文并且保留原本的 markdown 格式, fixed by adding "transition.pause( thisRock )" to the gameover function

通过在gameover函数中添加“transition.pause(thisRock)”进行修复。

2014-10-16 09:51:27
用户1925928
用户1925928

在重新启动时,必须删除所有运行时侦听器。否则,它们仍将处于侦听状态,这可能会导致错误,因为它们会导致已删除的方法。

将其放置在 RestartGame1:

Runtime:removeEventListener( "collision", onCollision )
2014-10-18 20:42:39