Adding collectibles

Collectibles are coins or other objects that the players can collect in the game. These collectibles often add something like scores or game coins. In our game, the collectibles will also be randomized. First, create a new star game object with a sprite from starGold in the Tappy Plane folder and put it in the Collectibles layer. Give it a Bullet behavior and put it outside the screen.

We will create new collectibles every few seconds instead of making them every time the game creates a ground object, like the rock obstacles object. To make collectibles, write the following code:

Adding collectibles

We also want to show what these collectibles collect. So, let's create a new text object; name it scores and put it in the HUD layer. Put it in the upper-left part of the screen; this is where HUDs usually are, although they can be in a variety of places. Now, when the player collects the collectibles, we want the collectibles to be destroyed and then add the score. So, create a global variable called score with a type of number, as shown in the following screenshot:

Adding collectibles

Now, add the following code:

Adding collectibles

The previous action sets the scores object's text to a text of Scores: followed by the value of the score global variable. The ampersand (&) symbol can be used to join text with a variable for a dynamic text. If you test it now, you can see the star created randomly, and collecting it will increase the scores. It's starting to look like a game; perhaps, it is only missing a start menu screen. If the stars move too fast for you, you can change its Bullet behavior's Speed property. Just remember to change the groundDirt object and rock's bullet speed to be the same value; if not, the star's movement will look strange.

Completing our code

Well, we've made our first game; that's great! However, there are things that are still missing. In a randomly generated scrolling game, any object that gets created needs to be destroyed at some point. If not, the objects that scroll off the screen will take up memory. As more and more objects are created, they will fill up memory space.

So, let's start deleting our objects; first, we'll destroy the rocks. Add a new event. We will compare the rock's X position and see if it is less than -100; if this is true, then set the action to destroy this rock:

Completing our code

This will destroy the rock. We'll do a similar thing with the ground, but instead of -100, we'll destroy it if its X position is less than -400. After comparing its X position to this position, add a destroy action as follows:

Completing our code

Finally, we'll destroy the star. As its size is smaller, we'll destroy it when its X position is less than -20:

Completing our code

Now, we have destroyed all the objects that have gone too far to the left of the screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset