Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Game development and iOS.

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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.