Creating a Timer with Corona SDK

In today’s post, we are covering another very simple topic, but one that’s essential to game development: a game timer. Some games will only allow 60 seconds to complete a level or a set amount of time to achieve a goal. So, here’s how you can create a timer with Corona SDK.

display.setStatusBar(display.HiddenStatusBar) _W = display.contentWidth _H = display.contentHeight number = 0
local txt_counter = display.newText( number, 0, 0, native.systemFont, 50 )
txt_counter.x = _W/2
txt_counter.y = _H/2
txt_counter:setTextColor( 255, 255, 255 )

function fn_counter()
number = number + 1
txt_counter.text = number

timer.performWithDelay(1000, fn_counter, 0)

Most of the code should be pretty self explanatory, but the code above is using a timer to add 1 to the counter in the middle of the screen. If you have questions or comments, please leave them below!

Daniel Williams

Leave a Reply

Your email address will not be published. Required fields are marked *