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

Create a Pinball Game: Adding Interaction

Read Time:6 minsLanguages:
This post is part of a series called Create a Pinball Game with Corona SDK.
Create a Pinball Game: Interface Creation

This is the second installment in our Corona SDK Pinball Game tutorial. In today's tutorial, we'll add to our interface and then code the game interaction. Read on!

Where We Left Off. . .

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

Step 1: 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 up in the next few steps.

Step 2: Add the Left Wall

Here we place the left wall, remember that it was split into parts to apply the physics later.

Step 3: Add Right Wall

This code places the right wall using the same method.

Step 4: Ball & HitLines

Add the ball and HitLines with the next code:

Step 5: Hit Balls

The targets are added using the next few lines:

Step 6: Paddles

Next we add the paddles and give them a name to use it later.

Step 7: Paddle Buttons

These buttons will control the paddles, add them using this code:

Step 8: Score TextField

Lastly, we add the score textfield.

Step 9: Wall Physics Bodies

You're probably wondering why we added the walls in parts. Since we are performing pixel perfect collisions on them, we cannot just use the addBody method (as this will add a box covering the alpha channel too) , instead we use the shape parameter to define a polygon for the image.

Why not a single polygon?

The shape parameter only accepts convex shapes and a maximum of 8 sides, our wall is neither, so we simply split it into pieces.

Step 10: Ball, Hit Areas, & Paddles

Similar methods are used with the other shapes, except for the circles that use the radius parameter.

Step 11: Top Wall

A simple line is added to the top of the screen as a wall to bounce the ball.

Step 12: Game Listeners

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

Step 12: Rotate Paddle

The buttons on the screen will trigger the rotation of the paddles. This code handles that.

Step 14: Return to Original Rotation

The rotation will return to its initial state when the button is no longer being pressed.

Step 15: Shoot

The ball will be shot when a collision with the paddle occurs, but only if the paddle rotation is active. This avoids shooting the ball when the buttons are not pressed.

Step 16: Increase the Score

The score will increase after a collision occurs with any hitBall.

Step 17: Check for Loss

This code checks if the ball falls through the space between the paddles and resets the ball if true.

Step 18: Call Main Function

In order to initially 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 compliler.

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. The icon 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 run 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.