How do I use timer.performWithDelay to call a function with parameters?

Okay so here is my problem: I am using timer.performWithDelay to call a function that has its own parameters but whenever I call that function the delay does not occur. For example:

    timer.performWithDelay ( 1000, fadeOut(splash, 750), 1 )

What this should do is after 1000 millisecond run the fadeOut function with parameters splash, and 750 one time but instead it just calls the fadeOut function right away without waiting the initial 1000 milliseconds. However if I remove the parameters and just call the function the function runs after the desired 1000 milliseconds.

    timer.performWithDelay ( 1000, fadeOut, 1 )

That code works, however it forces me to put the parameters in the function and causes me not to be able to reuse the function for other objects. This is the function I am calling:

1
2
3
4
    function fadeOut (variable, duration) --fades out an object over declared time
        local t = duration
        transition.to(variable, {time=t, alpha=0})
    end

Hi Joey, in this particular case, you could use the delay property in the param table you pass to transition.to()

function fadeOut( variable, t, delay )
transition.to( variable, {time=t, alpha=0, delay=delay} )
end

In general, you can pass function listeners (but not code) into performWithDelay. You must pass either a function listener or a table (object) listener. You can check out the Timer sample code for an example of table listeners in use with performWithDelay. It relies on the fact that a "timer" method exists in the object you pass to performWithDelay.

Hi,

I am seeking some further advice on this reply as this is something I'm failing to get the hang of.

I have a method, SetView that takes one param - the name of a view (referring to a background image that lives within a display group).

1
2
3
4
5
6
7
local function SetView(viewName)
   if (viewName == "settings") then
      viewGroup["settings"].isVisible = true
      viewGroup["main"].isVisible = false
   end
-- etc...
end

Walter does explain it.. but I think he could make it much more clear :)

You are doing it "wrong"... if you give 'SetView("board")' as assignment to "onComplete" you ask the computer to evaluate this function to return what the function is that gets called on completion.

Look at this:

1
2
3
local newText=display.newText -- Assignes the function
 
local newText=display.newText("mytext") -- Assigns the result of the function.

Additional... the "best" way to have something fadeIn/Out MOST of the time... is not using the onComplete stuff.. but using what walter said: the delay parameter of transition.to()

Example:

1
2
3
4
5
6
7
8
9
10
11
12
local myText=display.newText("Hello World!")
myText:setTextColor(255,255,0)
 
myText.alpha=0 -- invisible
 
-- fade in over 0.5 seconds
transition.to(myText, { time=500, alpha=1 })
 
-- wait 0.5 secs till it is faded in (500)
-- let it stay for 1/4 seconds (250)
-- then fade out over 0.5 seconds
transition.to(myText, { delay=500+250, time=500, alpha=0 })

thanks OderWat.. an anon function makes sense - very jquery like, this syntax.

Additionally, I already am using delay in my 'bounce' so I wasn't too sure how it could work in my favour without an onComplete.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if (event.phase == "press") then
   -- move the button a little to the left.
   transition.to(settingsButton, 
      {
         time = 100,
         x = settingsButton.x + 2,
         y = settingsButton.y + 1
      })
   -- return the button to its original position
   -- and then set a new background image (as defined by the "settings")
   transition.to(settingsButton, 
      {
         time = 100,
         delay = 100,
         x = settingsButton.x,
         y = settingsButton.y,
         onComplete = function() SetView("settings"); end
      })
end

I think that is a reliable and understandable way to code it.

Thanks again...

progress.. slowly but surely... :)

i'm sure my posts keep disappearing

just create a parameter on the timer

1
2
3
4
5
6
local function fadeOut(event)
print(event.source.something) -- "hi"
end 
 
local t2 = timer.performWithDelay ( 1000, fadeOut, 1 )
t2.something = "hi"

[deleted]

sorry there's some weird cross-posting function going on here!

excelent tip jmp909, solved my problem of animating a bomb fuse in extact 3 seconds.
now i call performWithDelay passing my fuse as argument and it calls it self for every next frame on a time fashion

views:3283 update:2011/10/5 21:23:48
corona forums © 2003-2011