Skip to content

Instantly share code, notes, and snippets.

@coronarob
Last active August 29, 2015 14:20
Show Gist options
  • Save coronarob/05accd04ce581b81572f to your computer and use it in GitHub Desktop.
Save coronarob/05accd04ce581b81572f to your computer and use it in GitHub Desktop.
A sample countdown timer
display.setDefault("background", 0.2, 0.2, 0.4 )
-- Keep track of time in seconds
local secondsLeft = 20 * 60 -- 20 minutes * 60 seconds
local clockText = display.newText("20:00", display.contentCenterX, 80, native.systemFontBold, 80)
clockText:setFillColor( 0.7, 0.7, 1 )
local function updateTime()
-- decrement the number of seconds
secondsLeft = secondsLeft - 1
-- time is tracked in seconds. We need to convert it to minutes and seconds
local minutes = math.floor( secondsLeft / 60 )
local seconds = secondsLeft % 60
-- make it a string using string format.
local timeDisplay = string.format( "%02d:%02d", minutes, seconds )
clockText.text = timeDisplay
end
-- run them timer
local countDownTimer = timer.performWithDelay( 1000, updateTime, secondsLeft )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment