How do I do a classic sleep() in Corona/Lua?

I’m really new to Corona and Lua. So far I’m really happy with both–although the lack of a ; at the end still makes me twitch :slight_smile:

In writing my first game, I’m trying to sync a lot of animations with what else is going on so have at least one function that needs to pause for a bit, then continue (e.g. start animation1, pause for 100ms, start animation2, then update some variables).

Seems like the way to do it is to use timer.performWithDelay(). However, this seems to continue running everything else in the function, so the order of execution ends up being: 1)start animation1, 2)update the variables, 3)start animation2

Here’s the code snippet, if that’s helpful:

 nextCardL:playClip("flip")  
 timer.performWithDelay(flipTime\*0.2, function() currentCardR:playClip("flip") end, 1)  
 timer.performWithDelay(flipTime, flipLCards, 1)  
 currentIndex = currentIndex + 1  

My assumption is that I’m just thinking about this wrong because I’m new to the language. Any suggestions?

Thanks. [import]uid: 136105 topic_id: 23964 reply_id: 323964[/import]

A couple quick notes:

You can include the ‘;’ if you want to. I always always do so because otherwise I get in a bad habit and forget them in other languages.

I’m not sure exactly what you mean by “2)update the variables” but there’s nothing stopping you from changing the variables that are going to be used following a delay, just make them local but outside the scope of your delayed function, change their values, and once the delay is triggered, you’ll have the updated values. Conversely, if you want one of your delays to use the original values just make a new local copy of the variable and pass it as a parameter in the delay function so it won’t be changed externally while you wait.

Corona is event based, so timers are your “sleep”.

I believe the answer to your question is duplicating variables (and potentially with a smaller scope depending on what you’re doing) so that they don’t get modified while you’re waiting. But if you have a more specific scenario in mind, let me know specifically what you’re trying to accomplish. [import]uid: 87138 topic_id: 23964 reply_id: 96528[/import]

For ex. this:

local clock = os.clock function sleep(n) -- seconds local t0 = clock() while clock() - t0 \<= n do end end [import]uid: 10141 topic_id: 23964 reply_id: 96532[/import]