2014-10-03 20 views
0

当托架被包括在该侦听器,用法()

timer.performWithDelay(delay, listener [, iterations]) 

timer.performWithDelay() 

未能延迟函数调用。

如果我尝试在

timer.performWithDelay() 

声明函数,它提供了语法错误。

那么如何使用timer.performWithDelay()中包含的函数传递参数/对象?

我的代码:

local normal_remove_asteroid 

normal_remove_asteroid = function (asteroid) 
    asteroid:removeSelf() 
    asteroid = nil 
end 

timer.performWithDelay(time, normal_remove_asteroid (asteroid)) 
+0

邮政的实际代码你正在使用。 – 2014-10-03 18:59:49

+0

已更新。请检查。 – Phoebus 2014-10-03 19:23:03

回答

0

在Lua中,()是函数调用操作(除了当旁边function关键字):如果对象是一个功能,或者具有__call方法,它会被称为立即。所以给normal_remove_asteroid(asteroid)performWithDelay实际上会给它normal_remove_asteroid函数调用的返回值。如果你想argumnents传递给函数,你必须创建一个临时的匿名函数不采取任何argumnents:

local asteroid = .... 
... 
timer.performWithDelay(time, function() normal_remove_asteroid (asteroid) end) 

在匿名函数的asteroid是个upvalue;它必须在该行上面的某处定义(因为您在normal_remove_asteroid(asteroid)中使用它,所以您的代码必须已经是这种情况)。

请注意,你不使用匿名函数:你也可以明确地定义一个包装的功能,但为什么还要将其命名,它只是包装:

function wrap_normal_remove_asteroid() 
    normal_remove_asteroid (asteroid) 
end 

timer.performWithDelay(time, wrap_normal_remove_asteroid)