1. Code
  2. Corona SDK

Create a Brick Breaker Game with the Corona SDK: Game Controls

This post is part of a series called Create a Brick Breaker Game with the Corona SDK.
Create a Brick Breaker Game with the Corona SDK: Application Setup
Create a Brick Breaker Game with the Corona SDK: Collision Detection

In this tutorial series, we’ll be building a Brick Breaker game from scratch using the Corona SDK. The goal of this game is to control a pong-like paddle to knock a ball against a stack of bricks until they all break.

Where We Left Off. . .

If you haven't already done so, please be sure to read part 1 of this series to fully understand the code in this tutorial.

Step 16: Function Declarations

Having declared a multi-dimensional table to hold our levels in Step 15, now declare all the functions that will be used in this app:

Step 17: Constructor Code

Now create Main(), the first function that will be called when our game starts:

Step 18: Add Menu Screen

The next code snippet adds the menu screen graphics to the stage and stores them in the menuScreen group:

Step 19: Button Listeners

Listeners are added to the buttons to perform the tweenMS function when tapped:

Step 20: Call About Screen

This function checks which button was tapped and displays the corresponding view:

Step 21: Hide Menu Buttons

These lines, the conclusion of the tweenMS function from above, hide the menu screen buttons to avoid unwanted taps:

Step 23: Remove About Screen

The next function tweens the about screen offstage and removes it:

Step 24: Destroy Menu Screen

When the user taps the start button we begin the game screen creation. The first thing to do is destroy the Menu Screen:

Step 25: Add Game Screen

Next we add the paddle and ball graphics:

Step 26: Call Build Level Function

Then we build the level. This function is fully explained later in the tut:

Step 27: Scores & Levels Text

The last graphics to add are for the score and levels text:

Step 28: Start Listener

A listener is added to the background. This listener will start the game when the background is tapped:

Step 29: Move Paddle

The paddle will be controlled using the device accelerometer. The data will be obtained using e.xGravity and passed to the x property of the paddle.

Step 30: Paddle Border Collision

To stop the paddle from leaving the stage, we create invisible borders on the sides of the screen:

Step 31: Build Level Function

The levels will be built by this function.

It uses a parameter to obtain the level to build, calculates its size, and runs a double for loop, one for the height and one for the width. Next, it creates a new Brick instance that is placed according to its width, height, and the number correspondig to i and j. The brick is declared as static in the physics engine as it will not be detecting the collision, that will be handle by the ball which is the only dynamic physics type.

Lastly, the brick is added to the bricks group to access it outside this function.

Step 32: Game Listeners

This function adds or removes the listeners. It uses a parameter to determine if the listeners should be added or removed. Note that some lines are commented as the functions to handle them have not yet been created.

Step 33: Start Game

In this function we call the gameListeners function that will start the movement and game controls:

Step 34: Paddle-Ball Collisions

When the ball hits the paddle, the ySpeed is set to negative to make the ball go up. We also check in which side of the paddle the ball has hit to choose the side where it will move next. The collision is detected by the collision event listener added in the gameListeners function:

Next in the Series

In the next and final part of the series, we'll be handling brick and wall collisions, scores, levels and the final steps to take prior to release like app testing, creating a start screen, adding an icon and, finally, 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.