timer.performWithDelay中的可变延迟时间

时间:2015-02-10 02:32:39

标签: timer lua corona

我需要在Corona SDK中调用具有可变延迟的函数。我正在尝试创建一个函数,该函数将执行一段代码,该代码检查timer.performWithDelay函数是否应该保持循环,并在条件合适时将其关闭。但是,该代码并不相关。我只需要一个延迟函数调用循环,其延迟时间可以变化。现在,我使用以下代码:

time = 500

local function foo( time )
    print( time )
end

timer.performWithDelay( time, function() 
                                time = time + 100
                                foo( time )
                              end, 10 )

显然,这不起作用。我认为幕后发生的事情是timer.performWithDelay只查看变量时间一次并将其用于其余的存在。有没有人在这种情况下有任何允许可变时间延迟的技术?

编辑:应用说明:我在骨架动画模块中使用此功能。当我播放一系列帧时,我需要能够随心所欲地暂停,恢复或取消剪辑,所以我需要一个延迟函数调用循环,每个函数调用都设置一组transition.to调用当前帧。 。 这意味着循环必须能够暂停,恢复和取消这就是我想在使用剪辑中的帧数作为循环编号时使用timer.performWithDelay的原因。我在下面列出了一个伪代码。

伪代码:

local flag = false

function loop( clip, object )
    for i = 1, number_frames_in_clip do
        timer.performWithDelay( duration_of_last_frame + time_elapsed, animate_object() )
        if flag == true then
            pause_loop()    <-- The real issue lies in pausing the loop
        end
    end
end

function pause( object )
    for i = 1, number_in_object.transitionTable do
        transition.pause( object.transitionTable[i] )
    end
    flag = true
end

1 个答案:

答案 0 :(得分:1)

你不能使用一个单一的计时器,但你可以这样做

local time = 500
local iterations = 1;
local currentTimer = nil;

local function PrintTime()
      time = time + 100
      iterations = iterations + 1

      print(time);
      if (iterations <= 10) then
           currentTimer = timer.performWithDelay(time, PrintTime);
      end
end

currentTimer = timer.performWithDelay(time, PrintTime);

以及每当您想暂停通话时

if (currentTimer) then
    timer.pause(currentTimer);
end

编辑:代码