Chapter 3
In This Chapter
Creating and destroying Objects
Setting Alarms
Using Steps
Making Instances collide
Using keyboard and mouse controls
Drawing to the screen
Enhancing your game with other Events
In this chapter, I cover each of the Events you can use within GameMaker: Studio. If you think of Objects, Events, and Actions in terms of English grammar, then the Objects are the subjects, the Events are the object (so to speak, just work with me here), and the Actions are the verbs. In that sense, the Objects are the things in the game (such as the player’s plane), the Events are what is acted upon (such as creating the plane), and the Actions make the magic happen (such as making the plane fly).
There are 12 different Events that you can use in GameMaker, as shown in Figure 3-1.
To add an Event to a new Object, follow these steps:
The Object Properties window appears.
The Events menu appears.
The name of the Event you chose appears in the Events section of the Object Properties window.
By the way, after you add an Event to the Object, you need to assign Actions to that Event. Chapter 4 is all about Actions.
The Create Event triggers an assigned Action that you want to happen at the start of the game or when the player enters a new Room. The Create Event is best used for initializing variables, starting Time Lines, setting paths, and so on. These are the types of Actions that you want to happen at the start of the game, or when an Instance first appears in the game, and you only need them done once.
You can also use the Create Event to provide an Object with an initial speed for when the Instance appears in the Room. This is useful if you make a game in which a plane flies continuously upward.
Another use for the Create Event is for when a bullet is fired. In that case, when the player presses the spacebar to shoot, that triggers a Create Instance Action (this is an Action, not an Event), which triggers the creation of the bullet Object, then within that bullet Object, is a Create Event. That Create Event is triggered when an Instance of that Object appears in the Room (when the player shoots a bullet) and the speed, sound, and movement Actions are triggered. Figure 3-3 shows the Create Event with the Actions for the bullet Object. Remember, all of these Actions happen when the Instance first appears in the Room.
Use the Destroy Event when you no longer want an Instance in the Room — say, when an enemy gets blown up. You can use the Destroy Event to trigger an explosion, increase the player’s score, or re-create a new Instance of the Object (or all three). Figure 3-4 shows an Object with a Destroy Event with a Set Score Action. This obviously isn’t a fully configured enemy Object — you would want the Destroy Event to be the last, and certainly not the only, Event.
You can add up to 12 Alarms for each Instance in the Room. Alarms are excellent for when you want to time Actions. For example, if you want the player to only shoot bullets once every two seconds, you can set an Alarm for that. If you want to time when new enemy planes appear, you can set an Alarm for that as well.
To get an Alarm to work, you must first set an Alarm Action to trigger the Alarm Event after a countdown that you set. In that way, Alarms enable you to time when events happen in your game.
When you choose the Alarm Event from the Event menu, a new submenu opens. From that submenu, you can choose the Alarm you want to configure and add Actions to (see Figure 3-5).
Step Events are great for when you want something to continuously happen, or you want something continuously checked for each step of the game. GameMaker tells time by steps, which is defined by the Room Speed (see Chapter 2), which sets how many steps are performed each second.
An example of using a Step Event is to determine if certain Instances have left the Room, and if so, to reappear at the top of the Room. As GameMaker needs to continuously check to see if the Instance left the Room, a Step Event comes in very handy to use.
When you click Step from the Event menu, a submenu appears giving you three options, as shown in Figure 3-6:
GameMaker updates all built-in variables between the Step and End Step. So, if you want something to happen after the variables have been updated, then choose End Step. If you want something to happen before the variables are updated, then choose Begin Step.
If your game has Instances flying every which way and collisions are inevitable, then you may be using a lot of Collision Events. These Events are great for when you want something to happen when two Instances collide — such as when a bullet Instance hits a plane Instance or, more simply, when a player Instance hits a wall Instance and you want the player to bounce off the wall (see Chapter 2).
Masks are important to Collision Events because this is how GameMaker determines where the actual collision takes place. (See Chapter 2.)
Mouse Events can be as simple as assigning the left button to fire, but there is a large submenu of Events that you can choose from (see Figure 3-7). Figure 3-7 also shows the tertiary menu for Global mouse. In this way, GameMaker provides you great control over how you want the mouse to behave in your game. After you choose which Mouse Event you want to use, you can add Actions to that event or even your own code.
Ah, the Others. Those not deemed worthy of their own place on the Event menu. I kid. These Other Events can be kinda awesome. The menu items (shown in Figure 3-8) are pretty much self-explanatory, so I won’t go into detail on them. For full descriptions of each of these items, you can always press F1 within the GameMaker and search for Events.
The Other Events are
Use the Draw Event when you want to display something during the game. The Draw Event does have its own submenu, as shown in Figure 3-9.
The different options from the submenu include
All Objects with an assigned Sprite with Visible toggled on (from the Object Properties window, as shown in Figure 3-10) automatically trigger a default Draw Event every step of the game. GameMaker does this for you, which is nice, because you don’t have to assign a Draw Event for each Object with an assigned Sprite to have it appear in the game.
The Draw Begin, Draw, and Draw End Events all run before the Draw GUI Events and between the PreDraw and PostDraw Events. This has significant meaning in that all these Events are drawn beneath the Draw GUI Event, no matter what the Depth (see Chapter 2) is set at.
You need to deselect the Clear Background with Window Colour check box in the Room editor for PreDraw and PostDraw Events to work. Figure 3-11 shows where to deselect this feature (the default setting has it selected). You also need to make sure that a background color is not drawn; you do this on the Background tab (see Chapter 2 for additional information).
If you turn off the View Clearing and disable drawing a background color, you might experience trails (leftover traces of images) during gameplay. These trails happen because now you’re drawing directly over the previous frame without it being cleared. You can use code to help solve this potential problem, which is draw_clear_alpha. You can place this code in an Execute Code Action within the PreDraw or PostDraw Event.
When you click Add Event, you may notice there are three different options for the keyboard: Keyboard, Key Press, and Key Release. Use the Key Press and Key Release Events to trigger an Action once each time an assigned key is pressed or released. The Keyboard Event is good for when the player needs to press the key continuously, such as for moving an Instance across the Room.
Each of the Key Events has a submenu, as shown in Figure 3-12, from which you can assign a key for the Action. Use <Left>, <Right>, and so on for the arrow keys on the keyboard.
The Keypad, Digits, Letters, Function keys, and Others have a tertiary menu. Figure 3-12 shows the third-level submenu for Letters — say, if you want to assign W for moving up, A for moving left, D for moving right, and so on (as opposed to using the arrow keys).
Also note that the keys for the numeric keypad only work if the player has NumLock toggled.
The Asynchronous Event is triggered at the end of another Action, such as the loading of a file or a reply from a web server. This Event also has a submenu, as shown in Figure 3-13.
Asynchronous Events are triggered by functions that need to receive data from a source at an unknown time in the future. This Event is best used for seasoned game developers who are creating complex online games.
In the following chapter, I go into detail on all the Actions that you can assign to Events.
3.15.3.167