简体   繁体   中英

Variable delay time in timer.performWithDelay

I need to call a function with a variable delay in Corona SDK. I am trying to create a function which will execute a chunk of code which checks to see if the timer.performWithDelay function should keep looping, and cuts it off if the conditions are right. That code is not pertinent, however. I just need to have a loop of delayed functions calls whose delay time can vary. Right now, I am using the following code:

time = 500

local function foo( time )
    print( time )
end

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

Obviously, this isn't working. What I think is going on behind the scenes is timer.performWithDelay only looks at the variable time once and uses that for the rest of its existence. Does anyone have any techniques that allow for a variable time delay in a situation like this?

EDIT: DESCRIPTION OF APPLICATION: I am using this function in a skeletal animations module. When I play a sequence of frames, I need to be able to pause, resume or cancel the clip at whim, so I need a loop of delayed function calls, each of which sets up the current frame with a set of transition.to calls. This means that the loop must be able to be paused, resumed and cancelled as well, which is why I thought to use timer.performWithDelay while using the number of frames in the clip as the loop number. I have laid out a pseudo-code below.

Psuedo-code:

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

you can't do it using one single timer, but you can do it like this

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);

and whenever you want to pause call

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

EDIT: code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM