Many of my readers may enjoy learning how they can create apps for their favorite mobile devices: ipad, iphone, or Android. The Corona SDK provides an elegant framework for quickly creating applications and games. I have heard stories of middle school students creating rocking games with this tool. So, I just had to check it out. In this five minute tutorial, I will walk through the process of creating a simple clock.
The Corona and Lua community have created an extensive collection of learning resources:
- Lua Tutorials: http://lua-users.org/wiki/TutorialDirectory
- Corona Tutorials: http://www.learningcorona.com/
- Corona Labs: http://www.coronalabs.com/
- Stories of indie game developers using Corona: http://www.indiegamepod.com/
- Clock tutorial that inspired this one: http://mobile.tutsplus.com/tutorials/corona/corona-sdk-creating-an-analog-clock-app/
Please let me know if you find this tutorial helpful.
What do you want to build today?
local function drawClock(e)
now = os.date(“*t”) — defaults to current date and timedisplay_width = display.contentWidth
display_height = display.contentHeightlocal background = display.newRect(0, 0, display_width, display_height)
background.strokeWidth = 3
background:setFillColor(20, 20, 20)
background:setStrokeColor(180, 180, 180)— draw circle
center_x = display_width / 2
center_y = display_height / 2
clock_radius = display_width /2local myCircle = display.newCircle( center_x, center_y, clock_radius)
myCircle:setFillColor(128,128,128)— draw hours hand
hour = now.hour
print(hour)
degree = -3.14/2 + (3.14*2/12)*hour
hours_x = center_x + (clock_radius-50) * math.cos(degree)
hours_y = center_y + (clock_radius-50) * math.sin(degree)
local hours_line = display.newLine( center_x,center_y, hours_x,hours_y )
hours_line:setColor( 0, 102, 102, 255 )
hours_line.width = 5— draw minutes hand
minute = now.min
degree = -3.14/2 + (3.14*2/60)*minute
min_x = center_x + (clock_radius-10) * math.cos(degree)
min_y = center_y + (clock_radius-10) * math.sin(degree)
local min_line = display.newLine( center_x,center_y, min_x,min_y )
min_line:setColor( 255, 102, 102, 255 )
min_line.width = 5— draw second hand
second = now.sec
degree = -3.14/2 + (3.14*2/60)*second
sec_x = center_x + (clock_radius-10) * math.cos(degree)
sec_y = center_y + (clock_radius-10) * math.sin(degree)
local sec_line = display.newLine( center_x,center_y, sec_x,sec_y )
sec_line:setColor( 1, 1, 1, 255 )
sec_line.width = 5
endtimer.performWithDelay(1000, drawClock, 0)