2014-10-03 33 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)