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:

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

Also if I call the function without the delay but the parameters it works as well.

 fadeOut(splash, 750) --this code works as well

So my real goal here is to be able to call a function with parameters in a timer.performWithDelay code. [import]uid: 6000 topic_id: 779 reply_id: 300779[/import]

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.
[import]uid: 26 topic_id: 779 reply_id: 1788[/import]

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

local function SetView(viewName)  
 if (viewName == "settings") then  
 viewGroup["settings"].isVisible = true  
 viewGroup["main"].isVisible = false  
 end  
-- etc...  
end  

When selecting a menu option I wish to animate (bounce) the menu item prior to showing the view image. Something like this… (just trying to move the button to begin with to keep it simple):

transition.to(settingsButton,   
 {  
 time = 1000,  
 x = settingsButton.x + 20,  
 onComplete = SetView("board")  
 })  

As Joey points out, the SetView function is called before the transition is complete due to it requiring a param.

Your statement about using a table makes some sense and I have played around with the Timer sample code but I don’t see how to implement my requirement using that code. I’m sure its a pretty standard pattern but Im at a loss so I would be grateful for some advice.

Coming from C#, I feel like Im missing a thread.sleep command so often so helping me over this hurdle would be awesome.

[import]uid: 7412 topic_id: 779 reply_id: 3814[/import]

Walter does explain it… but I think he could make it much more clear :slight_smile:

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:

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

To solve your problem you use the following construct:

transition.to(settingsButton,   
 {  
 time = 1000,  
 x = settingsButton.x + 20,  
 onComplete = function() SetView("board"); end  
 })  

which will create a temporary function which gets called on complete which then calls your “SetView()” function at the time you need it. [import]uid: 6928 topic_id: 779 reply_id: 3815[/import]

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:

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

Hope this makes sense…

P.S.: This Topic should be moved to the Developer Support section… [import]uid: 6928 topic_id: 779 reply_id: 3816[/import]

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.

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  

Is this the best pattern to adopt in your opinion?

Thanks [import]uid: 7412 topic_id: 779 reply_id: 3818[/import]

I think that is a reliable and understandable way to code it. [import]uid: 6928 topic_id: 779 reply_id: 3819[/import]

Thanks again…

progress… slowly but surely… :slight_smile:
[import]uid: 7412 topic_id: 779 reply_id: 3820[/import]

i’m sure my posts keep disappearing

just create a parameter on the timer

[lua]local function fadeOut(event)
print(event.source.something) – “hi”
end

local t2 = timer.performWithDelay ( 1000, fadeOut, 1 )
t2.something = “hi”[/lua] [import]uid: 6645 topic_id: 779 reply_id: 11544[/import]

[deleted] [import]uid: 6645 topic_id: 779 reply_id: 11545[/import]

sorry there’s some weird cross-posting function going on here! [import]uid: 6645 topic_id: 779 reply_id: 11547[/import]

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 [import]uid: 11078 topic_id: 779 reply_id: 20834[/import]