【问题标题】:Calling timer in a loop in Corona SDK在 Corona SDK 中循环调用计时器
【发布时间】:2014-02-26 06:01:08
【问题描述】:

无限次调用我正在使用的计时器

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 lua coronasdk


    【解决方案1】:

    如果你想无限次调用定时器,你可以使用:

    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),并在调用另一个/相同的计时器之前取消了可能正在进行的计时器。

    继续编码...... :)

    【讨论】:

    • 谢谢。我试过了,但仍然出现同样的问题。
    • @Akshada-Systematix:在调用下一个之前,请尝试取消所有其他计时器。这样就可以了。
    猜你喜欢
    • 1970-01-01
    • 2014-08-26
    • 2015-11-13
    • 2014-01-01
    • 2014-09-23
    • 2017-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多