Build a clock with Corona SDK

In this post, I’ll show you how to build a simple moving clock with Corona SDK. This tutorial uses display objects instead of images to keep things simple. Instead of walking through all of the pieces of code, I’ve listed the full script below. Feel free to drop this into your next project or modify as you see fit.

--Hide status bar from the beginning
display.setStatusBar( display.HiddenStatusBar )

-- Set Variables
_W = display.contentWidth; -- Get the width of the screen
_H = display.contentHeight; -- Get the height of the screen

clockBorder = display.newCircle(_W/2,_H/2,125)
clockBorder:setFillColor(0,0,0)
clockBorder.strokeWidth = 5
clockBorder:setStrokeColor(255,255,255)

hourHand = display.newLine(_W/2,_H/2,_W/2,80)
hourHand.width = 5
hourHand:setColor(0,255,0)

minuteHand = display.newLine(_W/2,_H/2,_W/2,47)
minuteHand.width = 3
minuteHand:setColor(0,50,255)

secondHand = display.newLine(_W/2,_H/2,_W/2,37)
secondHand.width = 2
secondHand:setColor(255,0,0)

local function updateClock(e)
 local currentTime = os.date("*t")
 secondHand.rotation = currentTime.sec * 6
 minuteHand.rotation = currentTime.min * 6
 hourHand.rotation = currentTime.hour * 30 + (currentTime.min * 0.5)
end

updateClock()
timer.performWithDelay(1000, updateClock, 0)

If you have any particular questions about the code above, please let me know in the comments.

Daniel Williams

Leave a Reply

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