Notes from J.A. Whye’s Mastering CoronaSDK Crash Course
Corona is a cross-platform game framework which can build games for iOS, Android, and soon for Windows. Games are built using a language called Lua and a text editor. No fancy drag-and-drop utilities are used. I prefer this, since it allows students to learn real programming and problem solving rather than just how to use as tool. Lua is easy to learn, very similar to JavaScript, and popular in the game industry. Read this blog post for more: http://blogs.msdn.com/b/matt-harrington/archive/2014/04/09/five-reasons-schools-should-teach-corona-sdk.aspx.
Prerequisites
- Register on http://coronalabs.com
- Download and install Corona. There’s a free version.
- Download and install Notepad++ or TextWrangler
- If you're on Windows, download and install Lua for Windows from http://lua.org. This isn’t strictly necessary, but it’s nice for testing Lua snippets.
- Skim this introduction to Lua: http://docs.coronalabs.com/guide/start/introLua/index.html. For a good introduction to Lua, watch the Lua course on http://www.pluralsight.com by Jon Sonmez.
- Read this Hello World tutorial: http://docs.coronalabs.com/guide/start/helloWorld/index.html
- Register for J.A. Whyte’s free Game Development Crash Course: http://masteringcoronasdk.com/game-development-crash-course/
Space Oddity
- Download zip archive. Show the fully built game.
- Rename
main-begin.lua
tomain.lua
- Walk through parts of
main.lua
display.newImage("background.png")
-- don’t put in functiondisplay.newImage("planet.png")
- Note that
planet
is at (0,0) local planet = display.newImage("planet.png")
planet.x = centerX
planet.y = centerY
- Do same for
background
Move on Down the Road
transition.to(planet, { time = 2000, y = 300 })
-- put in create playscreen region
Tweaking the Display Objects
planet.alpha = 0.2
-- explain what alpha isplanet.alpha = 0
- add
alpha = 1
totransition.to(planet…)
- Mention that display objects have lots of other properties, like
xScale
,rotation
, etc.
Are You Through Yet?
- Add this callback to
transition.to(planet, …)
:onComplete = showTitle
- Above
transition.to(planet,…)
create ashowTitle()
function local gameTitle = display.newImage("gametitle.png")
– inshowTitle()
- Run it
gameTitle.alpha = 0; gameTitle:scale(4,4)
-- inshowTitle()
transition.to(gameTitle, { time = 500, alpha = 1, xScale = 1, yScale = 1})
-- inshowTitle()
Touch-a Touch-a, Touch-a, Touch Me
- Touch vs. tap: touch events have phases. We’ll use tap
Tap
- Put all the code we’ve written so far into a
createPlayScreen()
function. - In
spawnEnemy()
put:
local enemy = display.newImage(“beetleship.png”)`
enemy.x = math.random(20, display.contentWidth – 20)
enemy.y = math.random(20, display.contentHeight – 20)
- Call
spawnEnemy()
afterstartGame()
. Run it a few times to see the random enemy position. - Move
spawnEnemy()
toshowTitle()
. You’ll get an error if you run. Putlocal spawnEnemy
into forward declaration region. MakespawnEnemy()
not local. enemy:addEventListener(“tap”, shipSmash)
-- add tospawnEnemy()
local obj = event.target ; display.remove(obj)
-- add toshipSmash()
- To stop processing an event listener, put
return true
in the callback
Bleep, Bloop! Pew, Pew!
- Put this in the
preload audio
region:
local sndKill = audio.loadSound(“boing-1.wav”) – in preload audio
local sndblast = audio.loadSound(“blast.mp3”) – in preload audio
local sndLose = audio.loadSound(“wahwahwah.mp3”) – in preload audio
- In
shipSmash()
, putaudio.play(sndKill)
Text
- Remove call to
spawnEnemy()
- Remove call at bottom to
startGame()
- Put this in
startGame()
:
local text = display.newText("Tap here to start. Protect the planet!", 0, 0, "Helvetica", 24)
text.x = centerX
text.y = display.contentHeight - 30
- Call
startGame()
at end ofshowTitle()
- Add an event listener to dismiss the text:
text:addEventListener("tap", goAway)
- Create
goAway()
:
local function goAway(event)
display.remove(event.target)
text = nil
spawnEnemy()
end