2 Adding The Ball

Section Menu

Breakout Home | 1 | 2 | 3 | 4 | 5 | 6

When the game starts, the ball moves around with the bat until the mouse button is clicked. This also happens when a life is lost or when a new level starts. Our plans will have to cater for this change in the game. We will start by defining the ball and making it follow the bat around.

Insert a new sprite called spr_ball and load the ball graphic. Create an object from this sprite and call it obj_ball.

Game Maker Screenshot

Game Maker Screenshot

Bring up the object window for the bat object. Add a Create event. Click on the Control tab on the right of the window and drag the Set Variable action into the actions window. We are going to have a variable called stuck and set its value to 0. Fill in the pop-up as shown.

Game Maker Screenshot

Giving you,

Game Maker Screenshot

When our variable stuck is set to 0, the ball will move with the bat, when we release it, the variable stuck will be set to 1

Now click on the Step event for the bat. We need to add some actions to make the ball move with the bat if it is stuck.

Go to Control tab and drag in the hexagon-shaped VAR icon (Test Variable).

We are going to start by checking if the ball is still stuck to the bat. Fill in the pop-up as follows,

Game Maker Screenshot

Drag in the arrow that points upwards (Start Block). Go back to the Move tab and drag the first Jump icon into the actions window.

Fill in the pop-up as shown.

Game Maker Screenshot

Add the down pointing arrow next (End Block).

Game Maker Screenshot

Go back to the Room you created and add the ball somewhere near to the bat. Save your work and run the game again. Check that the ball follows the bat.

The numbers in the diagram have come from trial and error and are based on trying to get the ball slightly above and in the centre of the bat as it moves.

Making The Ball Move

Now to get the ball moving when we click the left mouse button. We don't need the player to click on anything in particular so it doesn't matter which object we use to place the event.

We are going to use the bat object for this purpose.

Start by bringing up the object window for the bat object.

Choose to add an event to the bat object. You will need a Mouse Event and to choose Global Mouse, Global Left Button Pressed.

We start by seeing whether or not the ball is stuck. To do this, drag in a Test Variable icon from the control tab.

Fill it in as shown below to check if stuck is equal to 0.

Game Maker Screenshot

Add a Start Block arrow.

Immediately below the start block, add the Move Fixed action from the Main Tab (first icon you see). Fill in the pop-up as shown below.

Game Maker Screenshot

The settings in this window make the ball start moving in a randomly chosen direction based on the arrows that have been clicked.

Our final job is to set the value of the variable stuck to 1 so that the ball stops following the bat.

Drag in the Set Variable icon from the Control tab and fill it in as shown.

Game Maker Screenshot

Finish this section with an End Block arrow.

Game Maker Screenshot

Save your work and test this. When you click the mouse, the ball should go flying up and off the screen.

Some Fine Tuning

Test your game a few more times. Sometimes you click the mouse but the ball doesn't start moving. A way to fix this is shown in the diagram below.

Game Maker Screenshot

The actions from the highlighted action onwards check to see if the ball is not moving (its speed is 0). If the ball is not moving and is not stuck to the bat (stuck is equal to 1), the ball is moved to its position above the bat and stuck is set to 0 again. This means that you sometimes have to click more than once to make the ball move, but it always starts moving eventually.

Bouncing Ball

Now we need to make the ball bounce off the edges of the screen. In order to help us with testing, we will make the ball bounce off the bottom of the screen too. We can add the scores and lives later on.

Open up the window for the Ball object. Choose to add an event. Look under Other and choose Outside Room. This event fires when the ball object leaves the game room. The ball can leave the room from the sides or at the top or bottom. Go to the Control tab and drag the Test Variable icon into the actions window.

The first situation to check is if the ball has gone off the top of the screen. If the value of the variable y is less than zero, then this has happened and we need the ball to bounce back down the screen. In Gamemaker we can do this by Reversing The Vertical Direction. The screenshot shows how this has been done.

Game Maker Screenshot

Now to deal with what happens when the ball hits the left edge of the room. In this case, the value of x will be less than 0. We will then want to reversethe horizontal direction.

Game Maker Screenshot

The ball has gone off the right hand side of the screen when the value of x is greater than the width of the room. The following actions will do this for you.

Game Maker Screenshot

Finally, we need to make it bounce off the bottom of the screen. This will eventually be where we put the actions that take place when a player loses a life. For now, though, it's easier for us if you can't lose a life in the game. The final series of ball bouncing actions are shown below.

Game Maker Screenshot

Save your game and test the work you have done so far. The beginnings of the game are starting to appear. It feels strange when the ball passes through the bat. You want to try and knock it back up the screen. Then that is what we'll do next.

⇐ Previous | Next ⇒