1. Code
  2. Corona SDK

Corona SDK: Build a Space Shooter - Adding Interactivity


This is the second installment in our Corona SDK space shooter tutorial. In today's tutorial, we'll add to our interface and start coding the game interaction. Read on!

Where We Left Off. . .

Please be sure to read part 1 of this series to fully understand and prepare for this tutorial.

Step 1: Load Sounds

Sound effects used in the game will be loaded at start, this will make them ready for reproduction.

Step 2: Variables

These are the variables we'll use, read the comments in the code to know more about them. Some of their names are self explaining so there will be no comment there.

Step 3: Declare Functions

Declare all functions as local at the start.

Step 4: Constructor

Next we'll create the function that will initialize all the game logic:

Step 5: Add a Title View

Now we place the background and TitleView in the stage.

Step 6: Remove the Title View

The title view is removed from memory and the addShip function is called after.

Step 7: Show Credits

The credits screen is shown when the user taps the credits button, a tap listener is added to the credits view to remove it.

Step 8: Hide Credits

When the credits screen is tapped, it'll be tweened out of the stage and removed.

Step 9: Add Ship

When the Start button is pressed, the title view is tweened and removed revealing the game view, the ship movieclip will be added first by the next lines:

Step 10: Add Score

The following function creates and places the score text in the stage.

Step 11: Add Lives

The lives graphics are added by the next code, it also uses a table to store the lives number. This will help us later to detect when the player is out of lives.

Step 12: Listeners

In this function we add the necesary listeners to the interactive objects. We also start the timer that will add the enemies. A parameter is used to determine if the listeners should be added or removed.

Step 13: Move Ship

The ship will be controlled moving the finger horizontally across the screen. This code handles that behavior:

Step 14: Shoot

Tapping anywhere in the screen will make the ship shoot a bullet, this bullet is added as a physics object to detect its collision later.

Step 15: Add Enemy

The next function is executed by a timer every 800 milliseconds. It will add an enemy on the top of the screen. The enemy will be later moved by the update function.

Step 16: Alert

The Alert View will be shown when the user reaches a game state (i.e. win or lose), a parameter is used to determine which screen to display.

Step 17: Code Review

Here is the full code written in this tutorial alongside with comments to help you identify each part:

Next Time...

In the next and final part of the series, we'll handle the enter frame behavior, collisions, and the final steps to take prior to release, like app testing, creating a start screen, adding an icon and, building the app. Stay tuned for the final part!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.