Chapter 5
In This Chapter
Adding speed and movement with Views
Creating a Room to work with Views
Moving the View with a controller Object
Checking when the Object reaches the top of the Room
Placing a score display in a Room with a View
Keeping your player’s plane from flying off the boundaries of the View
Destroying an Instance when it leaves a Room
Guiding bullets toward a target
Creating enemy planes in a Room with a View
Arranging enemy planes in a Room
Moving on to the next Room
In this chapter, I introduce the concept of working with Views in your game. The basic concept of working with Views is that only a portion of the Room is seen at any given time. You set it up so that the Room actually moves down the screen along with Objects that you manually place in the Room.
For an example on how to use Views, I refer to the in-software tutorial Scrolling Shooter. In this tutorial, you begin by creating a background that moves down the screen to give an appearance of a scrolling background. All the enemies, islands, and so forth are randomly generated in the initial version of the game. In contrast, while working with Views, the Room itself actually moves down the screen, and the enemies and islands are manually placed in the Room instead of being randomly generated. This chapter focuses on creating a game with a View.
When working with Views, keep in mind that it is the View that has speed and moves, which affects what the player sees on his screen. Therefore, some Objects, such as the planes from the Scrolling Shooter tutorial, will have a vertical speed (to stay in view and not be dragged off the screen as the View moves), and other Objects, such as the islands, will not have speed (so that they move along with the View, and are dragged off the screen).
To add speed to an Object, follow these steps:
The Choose the Event to Add menu appears.
The Create Event appears in the Events section.
The Speed Vertical window, shown in Figure 5-1, appears.
A negative speed allows the Object to move up the Room; a positive speed allows the Object to move down the Room.
Now that the plane has some speed, you need to add speed to the Object you’re using for the bullet. You do this in a similar fashion as you did with the Vertical Speed Action. To add speed to the player’s bullets, follow these steps:
This Object was created as part of the Scrolling Shooter tutorial.
The Object Properties window opens.
The Create Event appears in the Events section of the Object Properties window.
The Vertical Speed window appears.
You want the bullet to move much faster than the plane.
The Vertical Speed window saves and closes.
The Object Properties window saves and closes.
When you create a Room to work with Views, you should do some planning ahead of time. Here are some questions to consider:
For example, you can have several Rooms in each game, and each Room can represent a level of your game. Therefore, each Room needs to be sized so that it’s big enough (plays long enough) so that you can incorporate each aspect of the level that you want.
Think of it this way: The Room can be a long, skinny column that appears to move down the screen. Only a certain portion of the Room — say, the bottom one-tenth — is seen at any one time. You then place Instances of Objects into the Room. For example, you can place a squadron of enemy planes directly outside the visible portion of the Room, so that they appear shortly after the start of the game.
To create a Room, follow these steps:
A submenu appears.
The Room Properties window appears.
For example, you may enter Level_1.
An Instance of the Object appears in the Room.
You can leave the default settings for X, W, and H.
These fields indicate the dimensions of the Room that is visible: Y for the top border; X for the left border; W for the right border; and H for the bottom border.
When you have your View configured, you can see the area of the View as indicated by a thin, white border that appears on the Room grid (refer to Figure 5-5).
The Room Properties window saves and closes.
If you want to see what you just created, you can run the game now to watch your plane fly up and leave the play area (I show you how to stop the plane from leaving the view later in this chapter).
You come back later to the Room to add in the enemy planes and the islands. But first, you need to create a controller Object to make the View move with a constant speed.
To create the controller Object, follow these steps:
The Object Properties window appears.
A step Event appears in the Events section.
The Set Variable window, shown in Figure 5-6, appears.
You just set a variable called view_yview and set the value to –2. Now, for each step of the game, the position of the View moves 2 pixels upward, which creates a scrolling effect.
Now you need to add a check to see when the Object (the player’s plane) reaches the top of the Room (Y < 0). Then the game should either stop or move on to the next level.
To add the check when an Object reaches the end of the Room, follow these steps:
The Test Variable Action properties appear.
You can now choose, via a drop-down list, which Object you want this Action to apply to.
The Object field populates with the name of the Object you chose.
GameMaker uses floating point values, which means the Y position may never actually hit 0. For example, the Y position may be 0.00000015. Therefore, you want to make sure to configure the Y value to less than the value of 1.
The Display Message Action properties window appears.
The Step Event should now look like Figure 5-7 and should provide a good test to see if everything is working. Later in this chapter, I show you how to update the Step Event so that the player moves on to another Room when he finishes the current Room.
To display the score in a Room with a View, follow these steps:
A Draw Event appears in the Events section.
The Draw Sprite Action properties window appears.
View variables are arrays, and as such, you should always use the View number when referring to them. However, GameMaker automatically selects view[0] when the array is not implicitly shown.
Now you need to add a Draw Score Action to the controller Object, by following these steps:
The Draw Score Action properties window appears.
Now you need to add a Draw Life Images Action to the controller Object, by following these steps:
The Draw Life Images Action properties window appears.
Now you need to add Draw Health Action to the controller Object, by following these steps:
The Draw Health Action properties window appears.
Currently, the player’s plane can fly right off the top and bottom of the screen. You probably don’t want that to happen in your game, so to prevent that, you’ll want to set up a Test Variable Action.
To set the top vertical boundary for an Object, follow these steps:
The Keyboard Event for Up appears in the Events section.
The Test Variable Action properties window appears.
The Jump to Position Action properties window appears.
Now, GameMaker will run a test to see if the player moves the plane to a height greater than the relative yview of the Room. If the Instance reaches a height greater than the height of the yview, the Instance performs the next Action after the Test Variable Action, which is the Jump to Position Action. The Jump to Position Action moves the Object down by 4 pixels to keep it within view of the player.
To set the bottom vertical boundary for an Object, follow these steps:
The Keyboard Event for Down appears in the Events section.
The Test Variable Action properties window appears.
The Jump to Position Action properties window appears.
Now, your Object should stop moving when it reaches both the top and bottom boundaries. You can also use this procedure for Left and Right Keyboard Events:
If your game involves bullets, or any other Object that you want destroyed when it leaves the Room, the following procedure was made just for you!
To destroy an Instance when it leaves the Room, follow these steps:
You can do this from the Resource tree.
The Outside Room Event appears in the Events section.
The Destroy Instance Action properties window appears.
Now, Instances of the Object that leave the Room will be destroyed.
If you want the enemy to have bullets that can track other Instances in the Room (say, the player), and you’re using Views, you need to use certain values for the targeting to work. I call these types of bullets guided bullets. In the following procedure, I show you how to add guided bullets to an enemy plane Object.
To create a guided bullet in a Room with a View, follow these steps:
The Object Properties window appears.
A Create Event appears in the Events section.
The Test Instance Count Action Properties window appears.
For example, if you want the enemy to shoot at the player, choose the player Object.
This tests that there is at least one Instance of the assigned Object in the Room. You can’t have the enemy shooting at a player if an Instance of the player Object is not in the Room (for example, if it has already been destroyed).
There needs to be one Instance of the Object in the Room. If there is, then GameMaker moves on to the next Action.
If you tell GameMaker to reference a variable that doesn’t exist (for example, to follow an Object that doesn’t exist), an error will occur and the game could crash. That’s why it’s important to test to make sure at least one Instance of the Object exists.
The Move Towards Action properties window appears.
Use the name of the Object that you want the bullet to chase after.
Use the name of the Object that you want the bullet to chase after.
You’ve now created a guided bullet Object that will track the player’s plane. To apply this bullet to an Object, such as an enemy plane that will shoot it, check out the next section.
In this section, I show you how to create enemy planes that work in a Room with a View. A couple of the attributes that you create include destroying the planes when they leave the Room and destroying all the bullets, such as the guided bullet described in the previous section, when they leave the Room. After you create the enemy planes, I show you how to add these planes to the Room.
To create enemy planes, follow these steps:
The Object Properties window appears.
A Step Event appears in the Events section.
The Comment Action properties window appears.
This is solely for your reference as you’re working. Comments are a great reminder of the Actions that follow.
Now you need to add a Test Variable Action to the enemy plane Object.
The Test Variable Action properties window appears.
You just set a Test Variable to see if an Instance of the Object is not within the View based on the Y coordinate. If it isn’t within the View, the next Action is triggered.
Now you need to add an Exit Event Action and a Test Variable Action to the enemy plane Object.
The Exit This Event Action appears in the Actions section. There is nothing to set for this Action. This is the next Action that is performed if the Test Variable Action is triggered. If the Test Variable Action is triggered, the Exit Event Action is triggered, which means no more Actions will be triggered for this Event (until the Test Variable stops being triggered and the Exit Event Action is not triggered as a result).
The Test Variable Action properties window appears.
You just set a Test Variable to see if an Instance of the Object is not within the View based on the Y coordinate. If it isn’t within the View, the next Action is triggered.
The Exit This Event Action appears in the Actions section and operates the same as in Step 1.
The Comment Action properties window appears.
You just created a comment that reminds you what the following group of Actions will do when triggered, which is that the Object is currently within view and the following Actions should trigger.
Now you need to add a Speed Vertical Action to the Object.
The Speed Vertical Action properties window appears. This is the Action that is triggered when the first two Test Variable Actions are not triggered due to the Instance being within view.
The Instance of the Object will now have speed and movement.
Now add a Test Variable Action to the Object.
The Test Variable Action properties window appears.
You just set a test to see if the Instance leaves the visible portion of the Room. If it does, then the next Action is performed.
Next, add a Destroy Instance Action to the Object.
The Destroy Instance Action properties window appears.
The Instance will now be destroyed if it leaves the Room, based on the Test Variable Action you set earlier.
The Test Chance Action properties window appears.
You just created a chance variable that will trigger the next Action with a dice roll’s chance of 1 out of every 80. That is, for every step of the game, GameMaker will roll an 80-sided die to determine if the next Action is triggered. If you want the next Action to be triggered more often (you want the enemy to shoot more often), lower this value, from 80 to, say, 20.
Now, add a Create Instance Action to the Object.
The Create Instance Action properties window appears.
You just created an Action that creates a bullet (obj_enemy_bullet).
Your Step Event for the enemy plane should look like Figure 5-8.
The enemy plane will now be destroyed if it leaves the View. Also, it won’t start to move until it enters the View. Plus, it has a sweet guided bullet tracking system set on full destruction of the player. (Insert evil laugh here.)
When using Views, you place the enemy planes in the Room, one by one, to form very cool formations. This is where you can let your imagination run wild in coming up with different patterns to challenge the player. The following procedure steps you through this process.
To add the enemy planes in your Room with a View, follow these steps:
The Room Properties window appears.
When you click in the Room, you place an Instance of the Object that you selected.
You now have a Room filled with enemy planes ready for the player to shoot out of the sky.
For this section, I assume that you have at least two Rooms created. If you have two Rooms ready to go, you’re ready to create a transition between Rooms.
To transition the player to the next Room, follow these steps:
The Object Properties window appears.
A Step Event appears in the Events section.
The Start of a Block Action appears in the Actions section. There is nothing for you to configure here. For more information on this Action, and all Actions, see Chapter 4.
The Go to Next Room Action appears in the Actions section. There is nothing for you to configure here. For more information on this Action, and all Actions, see Chapter 4.
The Go to Next Room Action appears in the Actions section. There is nothing for you to configure here. For more information on this Action, and all Actions, see Chapter 4.
You just set up a Step Event, with a series of Actions that will trigger when the Y coordinate of the player’s plane reaches the end of the Room. The Step Event should look like Figure 5-9.
You now have a game with two different levels that the player can advance through as she completes a Room. The player’s score carries over to the next Room because score is a global variable and will be relative no matter which Room the player is in.
3.22.77.63