You will use objects for such things as:
Making players and enemies move and interact
For placing and executing code
Checking and changing variables to make things happen
Collision detection
Processing mouse and keyboard input
Drawing variables and sprites
Creating sound effects and playing music
Objects will generally consist of several events. Code is placed in these events to make things happen within your game.
Objects are the items in the assets of the game assets tree in the IDE. When you place an object into the room it is referred to as an instance. This is an important distinction. For example, in the resources tree, you may have an object named obj_enemy, but have three instances of it in the room, each acting as separate entity.
The most important and commonly used events will be:
Create Event
which sets an initial value of hp and starts the instance moving in direction 90 at a speed of 5. It will continue moving at this speed and direction, until you tell it do something else.
Mouse Events
These events trigger upon input from the mouse, such as clicking a button or scrolling the middle mouse wheel.
Global mouse events allow actions to be performed if the mouse button is clicked anywhere on the screen, not just over the sprite of the object.
Standard mouse events trigger when clicked over the sprite assigned to the object (actually the mask set for the sprite).
A mask is the shape/area that will be used for collision events and functions. You can set a mask as a shape using the sprite editor, for example, as shown in Figure 6-2.
The detection of mouse input can be done through GML code in a Step Event (as shown in a previous chapter), or through use of events.
Quick Summary of the Mouse Events
Down Events – These events trigger whilst the mouse button is being held down.
Clicked Events – These trigger once when the mouse button is clicked.
Released Events – These trigger once when the mouse button is released.
Mouse Enter Event – Triggers once when the mouse enters the sprite area.
Mouse Leave Event – Triggers once when the mouse leaves the sprite.
Mouse Wheel Events – Triggers once when the mouse wheel is moved up or down.
Global Mouse Events – Work in a similar way to the top three events previously, but will trigger anywhere within the game window.
Destroy Event
This will play the sound snd_ouch once when the instance destroys itself.
Alarm Event
Alarms will trigger when they have counted down to 0. (They then go to -1 unless reset to a new value.)
An alarm will lose a value of 1 for each step of the game (in GameMaker there are by default 60 steps per second). So, an alarm set at 180 will trigger after three seconds. For example, the following would play a beep sound every two seconds.
This sets the alarm to twice the room speed, which is equivalent to two seconds.
This plays a sound then sets the alarm back to two seconds. It will countdown and repeat the sound every two seconds.
Draw Event
Your code actions for drawing should be put here, drawing text, shapes, or sprites.
which will draw the currently assigned sprite and index.
This will draw the currently assigned sprite and the text Score followed by the score.
Step Event
This will check if the health is below 0. If it is, it reduces lives by 1 and resets health to a value of 100. If the player is out of lives (equal to 0), the player will then be taken to the room room_game_over.
Key Events
These events work in a similar way to the key press code shown previously in this book. You may want to use code or events, do whatever you find easiest depending upon your game setup and design. Events can make it easier to organize your code, but perhaps harder to apply any changes needed.
There is nothing wrong in using keyboard events over GML code. In fact, sometimes it is preferable as it keeps your project more organized.
Keyboard Events work in a similar way to Mouse Events, with a distinction between down, pressed, and released.
Collision Event
A Collision Event happens when two instances collide (actually, when their collision masks collide).
Draw GUI Event
Basic Projects
- A)
Allow keyboard movement of a player object. Draw health as a value over the player. Draw this health in white, changing to red when the player's health is less than 20. Allow P and L keypresses to change the health.
- B)
Draw text that changes color each time the spacebar is pressed.
- C)
Create an object that changes color when the mouse hovers over it. Use a different subimage for each color.
Advance Project
- D)
Create a mini game with three instances of an object. Make them move in a random direction upon start. If player clicks them, award points and move in a new random direction.
Useful Functions
It will also get the value of size from the parent.
Summary
You should now understand commonly used events and what they are used for. You should have a strong basic understanding of the code you can put in such events.