Understanding Corona SDK cross platform development basics

Corona SDK is a cross-platform framework that empowers developers to create 2D games and apps for mobile, TV, and desktop using Lua. I blogged about Corona several years ago when it was a paid tool, but now it’s completely free, and based on Lua which is a fast language rather easy to learn. In this first of a new series of blog entries about Corona SDK, we’ll start from an advanced concept to place the basics of making a game. We will talk about the Composer. Composer is the official scene creation and management library in Corona SDK. This library provides developers with an easy way to manage individual scenes. If you are also a Phaser developer, thing about Corona scenes as Phaser states. We are going to build a script which creates a first scene with the game title and a button which will bring you to the game itself, which is just a text at the moment, this way: Let’s start from the beginning, so download Corona SDK and follow the Windows or OS X instructions then create your first “Hello World” project. Now you should have the requirements to start following this guide. First, as you can see from the project folder, the main script must be located in main.lua file. Unlike the examples you will find in the official docs, in our main.lua file this time we’ll place only two lines:
local composer = require(“composer”) composer.gotoScene(“title”)
Line 1: we declare a local variable called composer linked to composer library Line 2: this is how we load a scene, with gotoScene method and the scene name passed as argument. title is the name of the scene itself and also the game of the file which will contain the scene itself, title.lua:
local composer = require(“composer”) local scene = composer.newScene() function loadGame() composer.removeScene(“title”) end function scene:create(event) gameTitle = display.newText(“GAME TITLE”, 160, 100, “Arial”, 48) playButton = display.newImage(“playbutton.png”, 160, 300) playButton:addEventListener(“tap”, loadGame) end function scene:destroy(event) playButton:removeSelf() playButton = nil gameTitle:removeSelf() gameTitle = nil composer.gotoScene(“game”) end scene:addEventListener(“create”, scene) scene:addEventListener(“destroy”, scene) return scene
Line 1: same as before, we declare a local variable called composer linked to composer library Line 2: scene variable is set as a new scene with newScene method. Line 4: this is how we declare a function in lua. You’ll see we will call this function later at line 11. Also keep in mind there’s no { and } to declare function content. Line 5: removeScene method removes the scene. Basically this function removes the scene itself. Line 6: this is how we close a function. With end. Line 8: this function will be executed once the scene has been created Line 9: gameTitle variable displays a text, I think the arguments are self explanatory. Line 10: and this is how we display an image. Line 11: we are going to add an event listener, waiting for a tap then calling loadGame function. It’s the function I showed you before at line 4. This function will remove the scene, triggering the listener we are going to see. Line 14: this function will be executed once the scene has been destroyed Line 15: this is how we remove an asset Line 16: to completely free the memory, we also need to set its value to nil aka null in other languages Lines 17-18: removing the title, following the same concept. Line 19: calling game scene line 22: adding the listener to trigger when the scene is created Line 23: adding the listener to trigger when the scene is destroyed line 25: the script returns the scene itself What happens when the player taps the button? We simply call game scene which file is game.lua:
local composer = require(“composer”) local scene = composer.newScene() function scene:create( event ) local gameTitle = display.newText(“THE GAME”, 160, 100, “Arial”, 48) end scene:addEventListener(“create”, scene) return scene
And there’s nothing new in this file. That’s all at the moment, next time I’ll show you a very basic game made with Corona SDK built upon this example.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers