0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This code only executes function PrintTime() once. I am looking for something which will loop for a specified number of times. I have updated my question with a specific description of my application in case that helps you understand where I am coming from.
iBad, thanks for the help. I have gotten my code to work with an enter frame runtime listener and an anonymous function. I will give you an upvote and accept for the help though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.