by Ben Tyers
image_index=1;
image_speed=0;
Which would set the sub image 1 as the sub image to be drawn.
Note: The image index counting starts at 0. So if your sprite has just one image it will be index 0
Setting the image speed to 0 prevents it from automatically animating.
You can also set the speed the sub images will play at using, for example:
image_speed=2;
Which would set the animation speed at 2. The speed is a scalar value, so 0.5 will draw the same sub image for two steps, 0.25 for four steps, and that larger values like 2 will “skip” a sub image and only show every second sub image per step.
You can also set the angle of an image (its rotation). This can be a value between 0 and 359. For example:
image_angle=45;
You can set an object moving, for example the following will make the object move to the right at a speed of 2:
motion_set(0,2);
draw_self() is the same as draw_sprite_ext() using only all the default image variables, which is the same as letting GM default draw (i.e., no draw event defined, so GM draws the given sprite).
You can use draw_sprite_ext() with the default settings, for example:
/// @description drawing
draw_sprite(spr_test, 0, 200, 200);
draw_sprite_ext(spr_test, 0, 400, 400, 0.8, 1.2, 45, c_blue,1);
For example, the following will stretch the sprite 80% on the length and by 120% on its height; rotate by 45 degrees and blend with the colour c_blue:
Figure A_4_4 shows a sprite drawn normally, and with the code above:
Figure A_4_4: Showing sprite drawn normally, and with draw_sprite_ext
Note: Image blending works better with lighter sprites, and best with ones that are white.
Basic Projects
• A) Make a program that draws a rotating sprite.
• B) Make a program that writes a formatted message on the screen. Set a font type, colour, and alignment.
Advanced Projects
• C) Make a program that draws randomly positioned cloud sprites moving to the left at various speeds, with varying size and opacity (alpha).
• D) Get user to enter their name. Draw this on the screen, formatted, moving from the top of screen to the bottom. Destroy the object when it reaches the bottom
Appendix 5 Keyboard Input & Simple Movement
Keyboard interaction is one of the key elements of a game.
They can be used for:
Moving a player
Choosing a level to play
Changing game options
Setting cheat mode
Switching weapons
Picking up items
Note: Keyboard letters, that is, 'X' must be in capital when used with ord.
In addition to keyboard_check, there are other options available
. Note that there is a strong distinction between these three functions:
keyboard_check checks whether the key is currently being pressed.
keyboard_check_pressed checks whether the key has just been pressed.
keyboard_check_released checks whether the key has just been released.
Note: There is a strong distinction between these three functions – it is very important that you understand the difference and use the correct code when making your game
As well as keypresses, you can detect mouse button presses, also in the Step Event for example. As with key_check, there is a difference between mouse_check_button, mouse_check_button_pressed and mouse_check_button_released :
if (mouse_check_button(mb_left)) // Checks if left mouse button is being held down.
{
// do something
}
You can move an object by changing its x and y positions. x is the position in pixels across the screen, andy is how many down.
For example, you could put the following into a Step Event:
if (keyboard_check(ord(“A”))) {x-=5;}
if (keyboard_check(ord(“D”))) {x+=5;}
if (keyboard_check(ord(“W”))) {y-=5;}
if (keyboard_check(ord(“S”))) {y+=5;}
or
if (keyboard_check(vk_left)) {x-=5;}
if (keyboard_check(vk_right)) {x+=5;}
if (keyboard_check(vk_up)) {y-=5;}
if (keyboard_check(vk_down)) {y+=5;}
You can also use Boolean values as multipliers, since a value of false will return 0, and a value of 1 will return true, but this can be a bit confusing at first. The following allows you to move an object with key presses. vk_right is the built-in constant for the right-arrow key; it will return as true when the right-arrow key is being used. The same applies for the other arrow keys. You can combine keypresses in a cool way to make movement:
x+=5*(keyboard_check(vk_right)-keyboard_check(vk_left));
y+=5*(keyboard_check(vk_down)-keyboard_check(vk_up));
See Reference ➤ Mouse, Keyboard and Other Controls ➤ Keyboard Input in the GameMaker Studio 2 manual for more keycodes.
You can also get the value of the last key that has been pressed with keyboard_lastkey
Using keyboard_lastchar example, you make a string of what has been typed. In the Create Event of an object, obj_example put:
typed="";
In the Step Event place:
typed=typed+keyboard_lastchar;
keyboard_lastchar="";
And in a Draw Event put:
draw_set_colour(c_white);
draw_text(100,100,typed);
Put this object in a room and then test it.
There is an example for the above in the resources folder.
Basic Projects
• A) Make a movable object that can wrap around the screen, so if it goes off of the screen it appears on the opposite side.
• B) Create a simple two-player game, one player using WSAD and the other with arrow keys. One player must chase the other player around the room.
Advanced Project
• C) Create a maze that the player should navigate.
Note: You can check for the lack of presence of another object at a location using, for example:
if !place_meeting(x,y+4,obj_wall)
{
//do something
}
Appendix 6 Objects & Events
This appendix describes using objects and reflects what has been learned previouly. Objects are the lifeblood of GameMaker Studio 2. You use objects to do the following:
Make moving sprites
Insert code blocks of GML
Combine with events to make things happen
Detect collisions with other objects
Detect keypresses and mouse input
Draw sprites and variables on screen
Objects consist of events. You put your code in these events to create, change, detect, draw, or make things happen.
The main events you will use most often:
Create Event
This event is executed when the object is created or at start of the room if already present. This event is only executed once. It is useful for defining variables, and for any other sort of setup associated with new instances of the object, for example:
health=50;
lives=5;
Mouse Events
These are great for things such as creating an object when the mouse button is clicked, or changing the sub image of a sprite when mouse is over it. This can be used to execute code/actions if the mouse condition is true. This can be done using GML code Mouse Events.
Note: Global mouse events allows actions to be done 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).
Figure A_6_1 shows the Mouse Event options available:
Figure A_6_1: Showing available mouse events
Mouse interaction can also be done in GML in a Step Event: fo
r example, the following will play a sound when the left mouse button is released over the objects sprite:
if position_meeting(mouse_x, mouse_y, id) && mouse_check_button_released(mb_left)
{
audio_play_sound(snd_bounce,1,false);
}
The equivalent event for the above code would be Mouse Left Released Event, and the code would be:
audio_play_sound(snd_bounce,1,false);
Destroy Event
Code/actions in this event will be executed when the object is destroyed. It's great for changing global variables or playing a sound when it's destroyed. For example, when an enemy object loses all its health and you destroy the object, this can also be achieved in code:
instance_destroy();
In the Destroy Event you could put:
score+=10;
Note: It is worth noting that Destroy Events don't run upon changing rooms. This has several knock-on effects involving on-death effects and cleanup.
Alarm Event
Code / actions here will be executed when the chosen alarm reaches 0.
Alarms lose 1 for each step of the game. The default room speed is 30 frames per second. So an alarm set for 60 will trigger after 2 seconds. You can set an alarm using GML and then use an Alarm Event to execute code when the alarm triggers.
For example, you could use this as controller for a splash_screen to show a sprite for 5 seconds: In the Create Event:
alarm[0]=room_speed*5;
score=0;
lives=5;
global.level=1;
And in an Alarm0 Event:
room_goto(room_menu);
Alarm Events must be present for the corresponding alarm[ ] to count down.
Draw Event
Your code actions for drawing should be put here, drawing text, shapes, or sprites.
Note: It should be noted that wherever possible, only drawing code should be placed in a Draw Event.
If you have any code in a Draw Event you will also have to force the object draw the sprite, for example, using it in the simplest form:
draw_self();
You could add to this, for example, which would draw the score at the top of the screen with the caption Score :, and which ever sprite is currently assigned to the instance of that object:
draw_text(10,10,"Score "+string( score));
draw_self();
Note: The above code will draw the text first, then the sprite. If you want the text over the sprite, just change the order.
Step Event
Code/actions here are executed every step (frame). At the default room speed this will be 30 frames per second. This is most likely where you’ll use the most code. An example would be check the value of health and reduce lives accordingly, going to room room_game_over if the player is out of lives:
if health<0
{
lives-=1;
health=100;
}
if lives==0 room_goto(room_game_over); }
There may be times that you want to execute code before or after a main Step Event. For this you can use Begin Step or End Step accordingly.
Key Events
Will execute code/actions if a Key Press Event executes code or actions if the specified key is being pressed / released. In this book keypress events will be mostly checked using GML code. However you could use Key Press Events. An example would be creating moving an object 4 pixels right each time the left arrow key is pressed. To imphesise, the following code will execute once each time the right arrow is pressed:
if (keyboard_check(vk_right))
{
x+=5;
}
Note: The one-time nature of Key press and Key release events: they will not execute each step, instead only when the key is pressed or released.
If you want to make code execute every step while the key is being held down use:
if (keyboard_check(vk_right))
{
x+=5;
}
You can of course use Keyboard Events, as shown in Figure A_6_2:
Figure A_6_2: Keyboard events
Note: There is nothing wrong in using keyboard events over gml code. In fact, sometimes it is preferable as it keeps your project more organized.
Collision Event
Code in this section is executed if two instances (or their masks) collide. For this purpose of this book the Collision Event will be used more often than GML code, though GML does give more flexibility in how you process collisions.
You select which object to test for a collision with, and any code inside that event will be executed if a collision is taking place.
An example would be setting up a collision between obj_player and obj_enemy, as shown in Figure A_6_3.
Figure A_6_3: Setting up a collision event
In this event only, other can refer to the colliding instance.
For example, based on Figure A_6_3 above, the code could be that below which would reduce the hp of obj_enemy by 1:
with (other) hp-=1;
Then take one point off of the colliding instance's hp value for each frame (step).
Draw GUI Event
This event allows you to draw relative to the screen. It is mainly used for HUD elements, such as displaying the score, lives, bonuses, etc.
This draws independent of any view, so if the view moves, the GUI will not.
In most uses the GUI draws elements that cannot interact with the player.
Basic Projects
• A) Create a moveable player. Draw the health of a player as text in red above a player when health is less than
20. When over 20 draw in white. Set it up so P and L change the value of health.
• B) Make some text change colour, at random, each time the space bar is pressed.
• C) Create an object that changes colour when the mouse is over and when clicked on the object. Use a different sub image for each colour.
Advanced Project
• D) Create a mini game that randomly displays three objects that move in random directions when created and when clicked by the player. If objects go off side of screen, wrap around screen. Player is to click objects to get points and display points onscreen.
Appendix 7 Sprites
Sprites are images or sets of images that are assigned to objects. Sprites are images or multiple images. Multiple images can be used, for example, to create animations, or a change of image when a mouse cursor is over it. An example animation would be a character running. An example of a single image would be a menu button. Sprites are the graphic element of an object, which are displayed in game. There are lots of ways you can change how a sprite is drawn, which can be used to create various effects. Sprites can be used for the following such things:
Displaying player and enemies
Missiles and weapons
Walls and platforms
Menu buttons
Upgrade buttons
Lives
Moving objects
Collectibles
HUD
Backgrounds
You set an origin for a sprite. It is this point that will be used for displaying onscreen at an X and Y location. It is also worth noting that this point is also where transformations such as scaling and rotation are based around. It should be chosen carefully according to what the sprite will be used for. Figure A_7_1 shows the sprite origin set as center.
Figure A_7_1: Showing origin as center
Sprites can consist of single images or multiple images. Multiple images may be loaded in from separate images. Create a new sprite, spr_coins_1, and click import, as shown in Figure A_7_2:
Figure A_7_2: Setting name and selecting import
You can then select multiple images by holding down shift. Select all 8 images for the single coin, as shown in Figure A_7_3:
Figure A_7_3: Selecting multiple sub images
You can also import sprite sheets or strips. Create a new sprite, spr_coin_2 and click on edit, as
shown in Figure A_7_4:
Figure A_7_4: Naming a sprite and selecting edit
Then click image and Import Strip Image as shown in Figure A_7_5:
Figure A-7_5: Selecting import strip image option
You can then set as shown in Figure A_7_6:
Figure A_7_6: Setting the import properties
You can now delete the sprite spr_coins_2, by left clicking on it in the resources tree and selecting delete, as it won’t be used in the following example.
Set the origin to center for both the sprites.
Next create an object, obj_player, and assign the sprite spr_test from the resources folder for this appendix.
Place the following code in the Step Event:
/// @description Movement
if keyboard_check(ord("W")){y-=3;}
if keyboard_check(ord("A")){x-=3;}
if keyboard_check(ord("S")){y+=3;}
if keyboard_check(ord("D")){x+=3;}
That is all for this object.
Create an obj_coins_1 and assign the coin sprite, thenplace the following code in a Collision Event with obj_player:
x=irandom_range(16,room_width-16);
y=irandom_range(16,room_height-16);
The above code will chose a random value with the given range. 16 is used so that the coin does not appear over the room’s border.
Place one of each object in the room room0. Now test this game.
An example YYZ for this available in the resources folder
For other useful code, see the manual for usage; the first three are useful for checking a value as well as setting it.
image_angle – Can be used to set the direction (rotation of a sprite).
image_speed – How quickly a sprite’s sub images animate.