I defined random generation at the beginning of this chapter and now I will discuss it in a technical fashion. We will create the obstacles at a random position on the scrolling ground. So, first we will make the ground scroll.
To make the ground scroll, we need to first make it move in one direction at all times; to do this, we need to use a movement behavior. We'll use a behavior that is different from the 8 Direction behavior; to make an object move in one direction, we will use the Bullet behavior. Add this to the ground object, just like we added the 8 Direction behavior to the plane object. It doesn't matter to which of the two instances of the ground object you add the behavior; it will be added to all instances of this object.
Now, the ground will automatically move, so we will make it move in the direction we want it to. First, we will set the Set angle property from the Bullet behavior's property to No
because the ground doesn't change angle when moving. If you test the game now, the ground will move to the right; this is because the angle of motion of the Bullet behavior follows the sprite's angle. We want to keep the ground's angle at 0 degrees but change the Bullet behavior's angle of motion to the left, which is 180 degrees. So, go to the event sheet and add the following code:
This will make the ground move to the left, but it doesn't scroll yet. To make it scroll, we have to make a new ground when the ground created last reaches a position where it is necessary for us to make a new ground. To make it easier, the following screenshot denotes the position I am talking about; look at the selected ground, the one with the right-most position touching the game border:
Check out the properties of the groundDirt
sprite in the Properties bar; the X location we want is 450, so we want to create a new ground object when the object created last reaches this position. The question is how do we know that it is the ground object that was created last? The answer is through an instance variable. Create an instance variable named theLastOne
with a Boolean type for the ground object.
Now, we have two groundDirt
objects in the layout. Change the value of the theLastOne
instance variable of the right-most object to true
. Now, when the x position of the groundDirt
object with theLastOne
instance variable's value true
is 450 or less, we will create a new groundDirt
object. After that, we will change its theLastOne
value to false
so that it won't create another groundDirt
object. The code is as follows:
This code will create new ground at the position we want, which is (1220,448). Don't forget to set the Bullet behavior's angle of motion to 180 otherwise the newly created ground object will move to the right instead of to the left. Now I want to explain a bit about how Construct 2 picks the target of its actions in the event sheet.
In our previous code, we changed the value of the groundDirt
object's instance variable called theLastOne
twice; first, we set it to false
, and then, we set it to true
. This is because when we changed the value of theLastOne
the first time, there wasn't any groundDirt
object declared in this event block. The groundDirt
object that is manipulated is the one that fits the conditions written in the event block (the one with the theLastOne
value as true
and the X position less than or equal to 450).
After that, we wrote an action that creates a groundDirt
object at a position we want. If, after this, we manipulate a groundDirt
object (for example, change the value of its instance variable), the one that's manipulated is the latest one created. As you can see, besides just changing its instance variable's value, we also changed the angle of motion of its Bullet behavior, and the Bullet behavior that's changed is the one from the latest created groundDirt
object.
That's the basic information about picking objects in Construct 2. There is more to it, but I will explain this as we go along making our game. Next, we will create other obstacles randomly.
First, let's create a new rock object in the Obstacles layer; I used the rock sprite from the same free bundle folder. Add the Bullet behavior to it, just like you did with the ground obstacle, and set the Set angle property to no
so that it won't rotate. Now, put it somewhere off the screen because we don't want it to immediately appear on the screen. Instead, we want it to show up when we create a new ground object.
When we create an object at a random position, we need to remember the range of the position to create. First, the Y position is always at 360 because we don't want to create a random flying rock. Second, the range of the X position is from the left part of the ground object's sprite all the way to its right part.
To create a random value, Construct 2 has a built-in expression called random()
, which can be used to create a totally random value or a random value between a range of two values. To create a range between two values, we just need to input the two values inside the bracket, and the expression will pick a random value by itself. The left part of the groundDirt
object, when coded, is calculated using groundDirt.X – groundDirt.Width / 2, and the right part is calculated using groundDirt.X + groundDirt.Width / 2. We will edit the latest written code and change it as follows:
So, we added two new actions: one is to randomly create a rock object and the other is to move the z position of the rock object to the bottom of the layer. We moved the rock object because it looks better that way.
If you test it now, you can see the rocks are created randomly, and you can tap the screen to fly above them; a single mouse click is treated as a tap on the screen. However, you don't yet collide with the rocks; we also want the obstacles to stop moving when the plane collides with them and to make the plane fall down to the ground. To make the obstacles stop moving, we will do the same thing like we did when we tried to stop the plane from moving; we will disable its movement behavior.
This will make everything stop, but we also want to make the plane fall down. The plane can't flap up after this when the player taps on the screen, because the player already lost the game. We'll add a Bullet behavior to the bluePlane object, but we'll set the Initial state property to Disabled to make this property not work by default. Don't forget to set Set angle to No, and for this behavior, set the Speed property to 200
to make it slower. To make the plane go down, we just need to enable the Bullet behavior. Don't forget to set the angle of motion to 90
so that it will go down and change the angle of the plane so that it will look upside down.
If you test it now, the plane will go down after crashing into the obstacles. Next, we'll add collectibles to make our games more fun.