无限次调用我正在使用的计时器
function A()
print("Hello")
timer.performWithDelay(100, B, 1)
end
function B()
timer.performWithDelay(500, A, 1)
end
timer.performWithDelay(100, A, 1)
因此,如果我想在特定的时间间隔内打印 hello,我可以使用这两个函数进行调整。 但我面临的问题是一段时间后计时器变慢并非常快地调用函数 A 。 如果我做得对的话,有人可以建议我吗?要解决计时器问题我该怎么办?
提前致谢。
最佳答案
如果您想无限次调用计时器,您可以使用:
timer.performWithDelay(100, functionName, -1)
或
timer.performWithDelay(100, functionName, 0)
就您而言,您需要在再次调用之前取消计时器。因此,请执行以下操作:
local timer_1,timer_2,timer_3
function A()
print("Hello")
if(timer_1)then timer.cancel(timer_1) end -- cancelling 'timer_1' if exists
if(timer_3)then timer.cancel(timer_3) end -- cancelling 'timer_3' if exists
timer_2 = timer.performWithDelay(100, B, 1)
end
function B()
if(timer_2)then timer.cancel(timer_2) end -- cancelling 'timer_2' if exists
timer_3 = timer.performWithDelay(500, A, 1)
end
timer_1 = timer.performWithDelay(100, A, 1)
在这里你可以看到,我创建了计时器对象(timer_1、timer_2和timer_3),并在调用另一个/同一个计时器之前取消可能正在进行的计时器。
继续编码..............:)
关于timer - 在 Corona SDK 中循环调用计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22033075/