1. Code
  2. Corona SDK

Create a Retro Racing Game - Adding Interaction

Read Time:6 minsLanguages:

This is the second installment in our Corona SDK Retro Racing game tutorial. In today’s tutorial, we’ll add to our interface and the game interaction. Read on!

Also available in this series:

  1. Create a Retro Racing Game - Interface Creation
  2. Create a Retro Racing Game - Adding Interaction

Where We Left Off. . .

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

Step 1: Start Button Listeners

This function adds the necesary listeners to the TitleView buttons.

Step 2: Show Credits

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

Step 3: Hide Credits

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

Step 4: Show the Game View

When the Start button is tapped, the title view is tweened and removed revealing the game view. There are many parts involved in this view, so we'll split them over the next few steps.

Step 5: Place the Game Background

This code places the game background image in the stage.

Step 6: Add the Car

Next, we add the Car image and place it in the center of the stage.

Step 7: Create the Highway

This part creates the Highway lines, this image is added twice to create a loop. One is visible on the screen while the other waits in the top part of the stage.

Step 8: Add Text Fields

Now we place three textfields to display the Score, Level, and Speed.

Step 9: Create the Game Controls

Two buttons are added to use as controls for the car: left and right.

Step 10: Car Physics

Now we add the car to the physics engine, this way we can report collisions.

Step 11: Game Listeners

This function adds the necessary listeners to start the game logic.

This is a important part if you try to customize the game. The moveTimer controls the speed of the game. By default it is 100 milliseconds for Level 1, so level 2 may be 200 and so on.

The duration of the game is based in the number of obstacles that are presented in the level; this is defined by the oTimer. Modify the 15 value to create shorter or larger levels.

Step 12: Car Movement

This function controls the car. It tests the button pressed and moves the car to the chosen position if available.

Step 13: Add Obstacle

Obstacles are added by a timer based on a randomly calculated position which can be left, center or right. This function create an obstacle, gives it physics and then is added to a Group to make its movement and removal easier.

Step 14: Move the Highway

The update function is run by a timer to create the brick-by-brick effect. The next code loops the highway lines.

Step 15: Move Obstacles

This lines move the obstacles and remove the ones that are no longer visible. The score increases each time the car doesn't hit an obstacle.

Step 16: Level Completion

Here we check if all the obstacles defined are already out of the stage and call a win alert if true.

Step 17: Adding an Alert

The Collision function checks if the car is hit by an obstacle and calls a lose alert if true.

Step 18: Call the Main Function

In order to start the game, the Main function needs to be called. With the above code in place, we'll do that here:

Step 19: Add a Loading Screen

The Default.png file is an image that will be displayed right when you start the application while iOS loads the basic data to show the Main Screen. Add this image to your project source folder.

Step 20: Add an Icon

Using the graphics you created before you can now create a nice and good looking icon. The icon size for
the non-retina iPhone icon is 57x57px, but the retina version is 114x114px and the iTunes store requires a 512x512px version. I suggest creating the 512x512 version first and then scaling down for the other sizes.

It doesn't need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.

Step 21: Testing in the Simulator

It's time to do the final test. Open the Corona Simulator, browse to your project folder, and then click open. If everything works as expected, you are ready for the final step!

Step 22: Build

In the Corona Simulator go to File > Build and select your target device. Fill the required data and click build. Wait a few seconds and your app will be ready for device testing and/or submission for distribution!


Experiment with the final result and try to make your custom version of the game!

I hope you liked this tutorial series and found it helpful. Thank you for reading!

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