Corona SDK and other Game Development Tutorials
@gamebuilding

Creating a Clock with Corona SDK

Screen Shot 2013-03-07 at 6.49.34 PM

Late one night, instead of sleeping, I created a clock with Corona SDK. Creating a clock was a easier than I initially thought and I felt I should share the code on www.thatssopanda.com. So, here’s how I created a working clock with Corona SDK.

First, I created a new file called main.lua and added the following code. This allows me to hide the status bar and get the width and height of my screen.

--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

Next, we’ll add some display objects to our screen so we can see the clock outline, the hour hand, the minute hand, and the second hand. I used Corona’s built-in functions to create one circle and three lines. Each line has a different color and length so we can keep them separated.

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)

After the display objects, we create a function that will update our clock based on the current time. We can accomplish this by using os.date() and storing this as the current time. Based on the current time, we will rotate each hand to the appropriate location.

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

Then, we call the function so each clock hand shows in the right location.

updateCLock()

Finally, we call the function using a timer that goes off every second. By using the timer, we can make sure that the clock is being updated forever.

timer.performWithDelay(1000, updateCLock, 0)

That’s it! Corona SDK allows me to rapidly produce prototypes and although a clock by itself may not be very useful, you can clocks inside your game to provide a timer visual or in an app that needs timed events. I’ve provided the full source code to the clock below. The code is broken up into main.lua, config.lua, and build.settings.

Main.lua

--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)

Config.lua

-- config.lua
application =
{
	content =
	{
		width = 320,
		height = 480,
		scale = "zoomEven",
	},
}

Build.settings

settings = 
{
	orientation = 
	{
		default = "landscape",

		supported = 
		{
		"landscape",
		},

	},
}

If you have any more questions or comments, please let me know through the comments below. Thank you for reading another post from That’s So Panda!

4 Comments
  1. WOW! it Really Works for me thanks a Billion 🙂

  2. Thank you very much

  3. yeah bro its work i wanna ask something
    tell me hoe to set this clock as widget in our phone
    you have any codings

Leave a Reply