Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Corona SDK

Build an Air Hockey Game - Adding Interaction

Read Time:5 minsLanguages:

This is the second installment in our Corona SDK Air Hockey Game tutorial. In today’s tutorial, we will add to our interface and code the game interaction. Read on!

Also available in this series:

  1. Build an Air Hockey Game - Interface Creation
  2. Build an Air Hockey 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 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 in the next steps.

Step 5: Add Walls

Here we create 6 lines that will be used as the table walls.

Step 6: Add the Game Background

This code places the game background in the stage.

Step 7: Player

Add the Player paddle with the next code:

Step 8: Enemy

The Enemy paddle is created by this line:

Step 9: Scores

Next we add the scores TextFields to use them later.

Step 10: Puck

This is the puck, it will appear in the center of the stage.

Step 11: Add Physics

Lastly, we add the physics to the graphic elements. We also set a Puck property to false to prevent its rotation.

Step 12: Drag Physics Body

The next function handles the Player drag.

It uses a different method than previous tutorials as we want to drag the object without losing its physics interactions. You can read more about it in the Corona website.

Step 13: Game Listeners

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

Step 14: Move Enemy

A Timer is used to move the enemy, this will create a delay allowing the enemy to miss hitting the puck.

Step 15: Update Score

The Update function runs every frame.

This part of the code checks if the puck has left the stage through the space where no walls are added. If true, it increases the appropiate score textfield.

Step 16: Rest Puck Position

These lines reset the puck position when it leaves the stage and play a goal sound.

Step 17: Player Stage Border

Here we prevent the player paddle from going into the enemy part of the stage.

Step 18: Call 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: Loading Screen

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

Step 20: 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 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 find 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.