Multiple countdowns.

Hello,

I’m learning Corona and trying to build a small trivia app. I’m currently stuck at how to use two countdowns and then keep looping back to the first one. For example, when the game starts, the user will be presented with a question having 3 multiple choice answers. The user will see a countdown of 7 seconds. When the countdown reaches 3 seconds, 3 buttons will appear for the user to choose the right answer. At the end of those 3 seconds, that round will end with a 3-second pause ( countdown of 3 seconds) and then looping back to the 7 seconds for the questions with the cycle repeating itself. 

I was able to find a few topics on the forum about how to do a countdown. But I’m not sure how to make the second countdown check if the first one has ended and also how to make it a loop that repeats over and over again. Any help would be appreciated. Thank you :slight_smile:

Any ideas? I’d post what I had so far. I thought it’d be confusing, maybe it could help. I’ve just been at this for days now and I’m not sure how to make it work.

I set the question and answers in parse.com. I made a cloud code that randomly selects one of the question with the multiple choices and returns them the Android phone. I used the mod_parse.lua module by develephant. Here’s what I have:

local function onRun( event ) if not event.error then print( event.response.result[1].question )--Cloud code randomly pulls the question print( event.response.result[1].choiceA ) print( event.response.result[1].choiceB ) print( event.response.result[1].choiceC ) end end parse:run( "getRandomQuestion", functionParams, onRun )

If it’s too complicated, you don’t have to worry about displaying a countdown, I can do it myself later. I just need help with how to set up the timer. What I was trying to do:

local function timerListener10( event ) local function onRun( event ) if not event.error then print( event.response.result[1].question )--Cloud code randomly pulls the question print( event.response.result[1].choiceA ) print( event.response.result[1].choiceB ) print( event.response.result[1].choiceC ) end end parse:run( "getRandomQuestion", functionParams, onRun ) end local timer1 = timer.performWithDelay( 10000, timerListener10, 0 )

Since there will be a total of 10 seconds in the game, what I was trying to do was use something like “if( timer1 == 3000 ) then local function onRun( event )”. Basically after 3 seconds, display the question with multiple choice, leaving 7 seconds left on the timer. And then again, use another check for those 7 seconds, when the countdown now reaches 7000 milliseconds or 3 seconds left, to display the buttons. And then when the timer1 == 10000 or after the whole 7 seconds, to go back to the timer1 == 3000 or to loop and do the same thing over again. I tried putting that in my code but it was not working. Any help would be great. Thanks.

Hi @Bob The Builder,

If you want to continually “monitor” a time like this, it’s probably better to use a Runtime listener and check the system timer, so you know when it has reached a certain point. The main idea is, when you start the process, you set a variable to the current time since application launch (see link below). Then, on each timeframe iteration of the app, check the delta value (the current value minus the original value) and when it reaches the overall time that you want to check, i.e. around 3000 milliseconds, take whatever action necessary. Of course, remember that you shouldn’t check an exact time of 3000 milliseconds, but rather >= on that, since it would likely not hit exactly on 3000.

http://docs.coronalabs.com/api/library/system/getTimer.html

Best regards,

Brent

Any ideas? I’d post what I had so far. I thought it’d be confusing, maybe it could help. I’ve just been at this for days now and I’m not sure how to make it work.

I set the question and answers in parse.com. I made a cloud code that randomly selects one of the question with the multiple choices and returns them the Android phone. I used the mod_parse.lua module by develephant. Here’s what I have:

local function onRun( event ) if not event.error then print( event.response.result[1].question )--Cloud code randomly pulls the question print( event.response.result[1].choiceA ) print( event.response.result[1].choiceB ) print( event.response.result[1].choiceC ) end end parse:run( "getRandomQuestion", functionParams, onRun )

If it’s too complicated, you don’t have to worry about displaying a countdown, I can do it myself later. I just need help with how to set up the timer. What I was trying to do:

local function timerListener10( event ) local function onRun( event ) if not event.error then print( event.response.result[1].question )--Cloud code randomly pulls the question print( event.response.result[1].choiceA ) print( event.response.result[1].choiceB ) print( event.response.result[1].choiceC ) end end parse:run( "getRandomQuestion", functionParams, onRun ) end local timer1 = timer.performWithDelay( 10000, timerListener10, 0 )

Since there will be a total of 10 seconds in the game, what I was trying to do was use something like “if( timer1 == 3000 ) then local function onRun( event )”. Basically after 3 seconds, display the question with multiple choice, leaving 7 seconds left on the timer. And then again, use another check for those 7 seconds, when the countdown now reaches 7000 milliseconds or 3 seconds left, to display the buttons. And then when the timer1 == 10000 or after the whole 7 seconds, to go back to the timer1 == 3000 or to loop and do the same thing over again. I tried putting that in my code but it was not working. Any help would be great. Thanks.

Hi @Bob The Builder,

If you want to continually “monitor” a time like this, it’s probably better to use a Runtime listener and check the system timer, so you know when it has reached a certain point. The main idea is, when you start the process, you set a variable to the current time since application launch (see link below). Then, on each timeframe iteration of the app, check the delta value (the current value minus the original value) and when it reaches the overall time that you want to check, i.e. around 3000 milliseconds, take whatever action necessary. Of course, remember that you shouldn’t check an exact time of 3000 milliseconds, but rather >= on that, since it would likely not hit exactly on 3000.

http://docs.coronalabs.com/api/library/system/getTimer.html

Best regards,

Brent