Corona Starfighter with Corona SDK Part 2

Welcome to part 2 of building Corona Starfighter with Corona SDK! In part 2 of this game development tutorial, we’ll continue development of our game by building our game scene. Our game scene is where the action happens and you’ll do the following:

  • Initialize variables to make the game easier to manage
  • Create functions to stop the game, send the player back to the menu, and increase player score
  • Place graphics within the game


Before we can code our game scene, we need to create the file first! Create a new file called scene-game.lua and place this file in your project folder. Then, copy and paste the scene template from into the newly created file.

Libraries and Pre-set Values

With our scene setup, let’s start coding. We will start by adding in the widget library so we can add buttons. We’ll also call in the physics library so we can use it for collision detection. We will set the gravity to 0 since we are in outer space. Add the following lines after the composer library.

local widget = require( "widget" )
local physics = require( "physics" )
physics.setGravity(0, 0)

Next, add in the following variables to make it easier to manage our game. I’ve placed comments next to most of the variables to explain what each one will be doing.

-- These values are set for easier access later on.
local acw = display.actualContentWidth
local ach = display.actualContentHeight
local cx = display.contentCenterX
local cy = display.contentCenterY
local top = display.screenOriginY
local left = display.screenOriginX
local right = display.viewableContentWidth - left
local bottom = display.viewableContentHeight - display.screenOriginY

-- The next lines are forward declares
local createEnemyShips, timerForEnemies -- forward declares for function and timer
local enemyShip = {} -- a table to store the enemy circles
local enemyCounter = 0 -- a counter to store the number of enemies

local playerShip -- forward declare the player ship
local playerScore -- stores the text object that displays player score
local playerScoreCounter = 0 -- a counter to store the players score

local createBullets, timerForBullets -- variables to track bullet function and timer
local bullet = {} -- table that will hold the bullet
local bulletCounter = 0 -- although not necessary, I've added a counter to keep track of the number of bullets. This might be handy if you want to display how many shots the player took or player accuracy.

local onGlobalCollision -- forward declare for collisions. Collisions is what I use to detect hits between player-enemy and bullets-enemy.

Stopping the Game, Sending to Menu, and Increasing Player Score

Before we get to the scene:create function, we’ll add in 3 more functions. These functions will stop the game, send the player to the menu (when called), and increase the player score. We’ll start with stopping the game.

When the player gets hit, the function stopGame() will get called (in addition to gameOver(), but we’ll get to that later). This function is called to stop the game. It will stop bullets, stop enemies, and disallow movement by player. We will also set the stage focus to nil which restore default behavior. This will tell the app that the game is over and the player should no longer be interacting with the player ship.

local function stopGame()
 playerShip:removeEventListener( "touch", playerShip )
 display.getCurrentStage():setFocus( nil )

Next, we’ll need a quick function to send the player back to the menu. This function is called when the menu button in the top left is touched.

-- This is called when the menu button is touched. This will send the player back to the menu.
local function onMenuTouch( event )
 if ( "ended" == event.phase ) then

The third function that we will be adding is to increase the player score. The player score will get incremented by 1 whenever the player shoots down an enemy ship.

local function increasePlayerScore()
playerScoreCounter = playerScoreCounter + 1
playerScore.text = "Score: "..playerScoreCounter

Adding Some Graphics

In this section, we’ll add in a background, a top bar (just for some polish), a player score, and a button. The background is a simple background that will make the player feel like they are in outer space. The topbar is a simple bar that goes across the top of the screen and I placed it there to make the game feel more polished. The player score will be a visual indicator of how many ships the player has shot down. And finally, the menu button will send the player back to the menu when touched.

-- Create the background
local background = display.newImageRect(sceneGroup, "images/background.png", 475, 713)
background.x = cx
background.y = cy

-- Create the black bar at the top of the screen
local topbar = display.newImageRect(sceneGroup, "images/topbar.png", 475, 31)
topbar.anchorY = 0
topbar.x = cx
topbar.y = top

-- Create a text object to keep track of the player score
playerScore = display.newText(sceneGroup, "Score: "..playerScoreCounter, 0, 0, native.systemFont, 20)
playerScore.anchorX = 1
playerScore.x = right - 5
playerScore.y = topbar.y + 15

-- Create a button to allow the player to return to the menu
local btn_menu = widget.newButton({
 label = "Menu",
 fontSize = 24,
 labelColor = { default={ 1, 1, 1 }, over={ 1, 1, 1, 0.5 } },
 onEvent = onMenuTouch
btn_menu.x = left + 40
btn_menu.y = topbar.y + 15

Don’t forget, you’ll need to grab the game template if you’d like to use the graphics that I have. And that’s it for part 2 of this Corona SDK game tutorial! Stay tuned for part 3 where we will add in the player ship and make it respond to touch events! Otherwise, please leave any comments or questions below.

Daniel Williams

Leave a Reply

Your email address will not be published. Required fields are marked *