Understanding constant variables

Now, we will start changing the game state. We will do this by utilizing two kinds of variables: instance variables and global variables. We'll make several global variables that will be checked later with an instance variable. However, we will change the global variable to a special type of variable: constant variable.

Note

So, what are constant variables? They are variables, global or local, whose values do not change after they are declared. Both the text and number type variables can become constant variables. To make a constant variable, just select the Constant checkbox when creating a new global variable. Constant variables in the event sheet are indicated by their names, which appear capitalized.

Let's try it now. Go to the event sheet and create a few variables needed to create the blocks' special effect. As you remember, the default value for the blocks' effect instance variable is 0; so we'll make a value of zero when the block has no special effect at all. Then, we'll set incrementing values for the other special effects, as shown in the following screenshot:

Understanding constant variables

This is the bonus that players get when a certain block is destroyed. So, let's change the value of the effect property of the blocks in the layout. Give a value of 1 to the green blocks, 2 to the red blocks, and a value of 3 to the yellow blocks.

Adding sub-events

Now that we've assigned values to the effect property, we can start adding code to the event sheet. The logic is if the block is destroyed, we'll check the value of its effect instance variable; if it is the same as one of the constant effect global variables, we'll change the value of a variable depending on the effect. To do something when an object is destroyed, we will use the On destroyed event. Let's add an On destroyed event to the event sheet.

Now, we want to compare the value of an instance variable after an On destroyed event; this means that we want to create an event inside another event, or in other words, we want a sub-event. To create a sub-event, first select the event you want to make a sub-event from and then press S on your keyboard. A new window will open up; here, you can select a new event for the sub-event. Create your sub-event until it looks like this:

Adding sub-events

Once again, because adding an image every time I teach you a code will be troublesome, this is how I write it in the code form:

Adding sub-events

Note

Note that there's no limit on how many sub-events can be made inside an event. You can create a sub-event inside a sub-event, but it is suggested that you do not make too many sub-events because the code will be too confusing to understand.

Now, to make these blocks really work, we need to make three global variables: score, life, and gameTime; all are number types. For life and time bonuses, we want them to increase after a fixed number; life will increase by 1, and time will increase by 10 seconds. For score bonuses, we'll use a simple formula to make it vary per player. The score bonus will be based on the player's life and the current time remaining; the code will be as follows:

Adding sub-events

We have successfully added special effects that give a bonus when the blocks are destroyed. Now, what if we want to change something about the paddle when a block is destroyed?

Changing a game object's state

Other than adding bonuses to the game, we can make a special effect that changes the state of an object. In this example, we will change the state of the paddle. While changing the state of the paddle, we will determine the duration of the new state, and after this duration ends, the paddle will revert to its original state. To make it simple, the duration of this new state stays until the player destroys another block that doesn't have a special effect. For now, we'll make the paddle wider.

To do this, we'll create a Text type instance variable called state on paddleBlue. The value of this instance variable will determine the state of the paddleBlue object. Now, we need a place to change it; let's change it when the purple block is destroyed, because right now, nothing happens when we destroy it.

To select a purple block, we'll take a look at its blockColor instance variable; if it says purple, then we got the block we want. After that, we can change the state of this block. One more thing we want to do is change the paddleBlue state back to its default value. We can do this by checking both the effect and the blockColor instance variable to find a block that isn't purple and has no effect. So, we will add these sub-events to the latest written code:

Changing a game object's state

We have changed the state of the paddleBlue object; now, to make this really work, we need to add two more events: one when the state instance variable's value is wider and another when it is an empty string. We want to make it wider when the state is wider and return the width to normal when it is an empty string. Note that that the paddleBlue object's normal width is 104 pixels wide, and we want to make the width 184 pixels wide when it's wider (or you can set your own value). The first event will be as follows:

Changing a game object's state

The second event will be as follows:

Changing a game object's state

If you test the game now, you can see that the paddle becomes wider after the purple block is destroyed and reverts to normal when the blue block is destroyed, but the paddle stays wide when the destroyed block is not blue.

Adding more states

You already know how to change a state of an object and revert it to its original state. However, what happens when you add state-changing blocks? We'll answer this by giving another state to the paddle. First, let's add another animation called Grey to the block's object; give this animation a gray rectangle sprite.

Just like we did earlier, for some blocks, change the blockColor instance variable's value to Grey. We want to change the angle of the paddle after the player destroys this block, so first, we will add another sub-event when the block is destroyed.

Adding more states

Just like we did earlier, to actually make the state have an effect, we will make another event that checks the paddleBlue state. This new event will set the angle of the paddle if the state value is angle:

Adding more states

Don't forget to change the angle back to 0 degrees when the state is empty again:

Adding more states

Test your game now; if you destroy the purple block and then the grey block, or the other way around, you'll find that the paddle becomes big and tilted at 45 degrees. This is because two effects of state-changing blocks are applied.

As you can remember, we only changed the width and angle of the paddle back to its original value when the state instance variable is an empty string. This time, the state instance variable didn't have a chance to become empty before the state change, thus applying two state changes at the same time.

Tip

When making your game, always look out for small gotchas like this. This is because Construct 2 doesn't always know what you want to do, which is a common thing when making a game, no matter what tool you use. This usually happens when you change the value of a variable.

To fix this, we need to deactivate other state-change effects when we switch to a new state. We'll set the angle to 0 when we make the paddle wider, and we'll change the width back to the original one when we tilt the angle. So, we'll add two new actions to two different events. The first event will be as follows:

Adding more states

The second event will be as follows:

Adding more states
..................Content has been hidden....................

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