Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 尝试调用全局';gameover';(表值)_Lua_Coronasdk - Fatal编程技术网

Lua 尝试调用全局';gameover';(表值)

Lua 尝试调用全局';gameover';(表值),lua,coronasdk,Lua,Coronasdk,所以我正在使用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 o

所以我正在使用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
,它有一个表值而不是函数值。如果运行以下操作,则会出现相同的错误:
gameover={};gameover()


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

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

范例

el = {}

print(el)
table: 0x7f8fe9e003f0

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


el.f1()
hola

通过在gameover函数中添加“transition.pause(thisRock)”修复了此问题。重新启动时,您必须删除所有运行时侦听器。否则,它们仍将侦听,这可能会导致错误,因为它们会导致已删除的方法

将其放入RestartGame1:

Runtime:removeEventListener( "collision", onCollision )

在问题中添加了gameover函数
Runtime:removeEventListener( "collision", onCollision )