Corona Starfighter with Corona SDK Part 1

Hello future game developers!

There’s a lot of great tutorials out there for Corona SDK and they cover a variety of topics from card games to memory match games. With all of these great tutorials, I wanted to throw in a classic space shooter style game. This game, called Corona Starfighter, consists of two scenes – the menu and the gameplay scene. The player’s task is to stop enemy ships by shooting them down and for each enemy ship shot down, the player will earn one point. The players ship is controlled by a touch event and the player can drag his or her finger left or right to move the ship. When the player gets hit by an enemy ship, it’ll mean game over!

During this tutorial, you’ll learn some real life application of the following topics:

  • Scene management with Composer
  • How to use widgets
  • Using physics to add bodies and detect collisions
  • Shoot bullets
  • Move a player ship based on a touch event
  • Send enemies using timers
  • And trigger a game over event

If you’d like to use the images that I have for Corona Starfighter, please download the full game template at http://gamebuildingtools.com/product/corona-starfighter-corona-sdk. This digital download provides all of the images that I used as well as the final project. Let’s get started with this game tutorial!

Setting up our project

To setup this project, I used Corona’s built in project creator. Open Corona, go to new project, and set the width to 800 and the height to 1200. Below is a screenshot of the project setup. I’m on a Mac, so your setup may look a bit different.

Project setup

main.lua

After your project is setup, open up main.lua in your favorite text editor and add in the following code:

-- Hide the status bar
display.setStatusBar( display.HiddenStatusBar )

-- Go to the menu scene
local composer = require( "composer" )
composer.gotoScene("scene-menu")

main.lua is the starting point of every Corona build app and adding this code will hide the status bar. We will also send the player to the menu scene using the composer library. The composer library is Corona’s built-in scene management tool. A basic way to explain scenes is that each screen is equivalent to a scene. Therefore, the menu is a scene and the game play is a different scene.

scene-menu.lua

Next, create a new file in your project folder called scene-menu.lua. Instead of starting from scratch, I like to utilize the scene template from https://docs.coronalabs.com/api/library/composer/index.html#scene-template. This scene template provides a framework for us to use and copy everything from local composer = require( "composer" ) to return scene and copy this to scene-menu.lua.

Once you have the code copied, add the widget library after the composer library creates a new scene.

local composer = require( "composer" )
local scene = composer.newScene()
local widget = require( "widget" )

Then, I like to create shortcuts for commonly used variables for content width and content height. Place this code below the widget library.

-- 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 bottom = display.viewableContentHeight - display.screenOriginY

After we have our initial variables setup, we’ll add in a function that will respond to the play button being touched. This function will whisk the player off to the game scene.

-- This function will send the player to the game scene
local function onPlayTouch( event )
 if ( "ended" == event.phase ) then
  composer.gotoScene("scene-game")
 end
end

Some of us like visuals, so here’s a snapshot of the code so far for scene-menu.lua. This will show you where I’m placing the code in scene-menu.lua.

Code so far

Within the function scene:create(), we’ll add a background, a title, and a button widget. Please take note of the comment I added to the background graphic. Corona Starfighter is compatible with the various mobile devices out there and making the background larger accommodates the different sizes.

local background = display.newImageRect(sceneGroup, "images/background.png", 475, 713) -- I make the background larger than the width/height specified in config.lua. This allows the entire device screen to be filled up with the background graphic without stretching it.
background.x = cx
background.y = cy

-- Create the title
local title = display.newImageRect(sceneGroup, "images/title.png", 300, 230)
title.x = cx
title.y = 175

Following the title, we’ll create a start playing button. When the button is touched, the function onPlayTouch is triggered and the composer.gotoScene() is called. This widget is the last piece of code added to scene:create. The next part will be added to scene:show().

-- Create a start playing button. When touched, trigger the onPlayTouch function
local btn_play = widget.newButton({
 left = 100,
 top = 200,
 defaultFile = "images/btn_startplaying.png",
 overFile = "images/btn_startplaying_over.png",
 onEvent = onPlayTouch
})
btn_play.x = cx
btn_play.y = cy + 100
sceneGroup:insert(btn_play)

Finally, we’ll add a remove scene call to the function scene:show(). This will essentially reset the game scene so the player gets a fresh game every time. Otherwise, the gameplay scene will never get reset.

local prevScene = composer.getSceneName( "previous" )
if(prevScene) then
composer.removeScene( prevScene )
end

And that’s it to part 1 of building Corona Starfighter! In this game tutorial, you’ve learned how to build the menu scene by adding in the widget library, some graphics, a button, and how to clean up a different scene.

If you’d like to skip ahead, download the full source for this game template at http://gamebuildingtools.com/product/corona-starfighter-corona-sdk. Otherwise, check out part 2!

Daniel Williams

2 thoughts on “Corona Starfighter with Corona SDK Part 1

  1. Pingback: How To Shuffle A Table in Lua - Game Building Tools

  2. Pingback: Creating a Fire Button with Corona - Game Building Tools

Leave a Reply

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