Moving your player
There are many different types of games with different visuals and play styles. Some games are devoid of a visible player character but most, especially 2D games, have the player controlling a sprite or 3D model onscreen. We're going to take a quick look at putting a character in front of the player that he/she can control.
Getting ready
You've animated some sprites, but animated sprites won't do you any good without a character to which they can be applied. Create a new object and call it obj_player
. Under Sprite, click the menu icon and select spr_player_idle
.
How to do it...
- In
obj_player
's Object Properties window, click Add Event, then click Keyboard and select<Right>
from the menu. This event will allow you to make things happen while you're pressing the right arrow key on your keyboard. What we want to have happen is for our player character to move to the right and to have his right facing walk cycle play while he is doing it, so let's do it. - Under the Main1 tab on the right side of the Object Properties window, drag Change Sprite (it looks like a green Pac-Man) and drop it in the Actions box.
- The actions we want apply to
obj_player
, so select Applies to Self. - Since we want to use
spr_player_walk
in this case, select the sprite from the drop-down menu. Subimage
defaults to0
, but this tells GameMaker that we only want to show the first frame of the animation. Since we want to have the entire animation play, you'll need to set this value to-1
, which tells GameMaker to ignore the subimage count.- The sprite's play speed can be altered by setting the Speed option to a value between
0
and1
. Set the value to0.8
so that the walk cycle will play at 80 percent of the room's frame rate and close this box.We want the player character to move right when the right arrow key is pressed but we don't want him to simply walk right off the screen. How mad would you be if the character you were controlling simply gave up, said That's it, I'm out of here! and disappeared off screen? Sounds like an entirely frustrating experience. Since we don't want this to happen, let's make sure it can't. For that, you'll need to add a variable check before your character goes anywhere.
- Under the Control tab, drag and drop Test Variable to the Actions box.
- We want to test the variable
x
, which is the player's x coordinate or horizontal position. Since we want to the player to stay on-screen, we'll set thevalue
toroom_width-16
and theoperation
will beless than
. - Close the Test Variable box and under the Move tab, drag and drop Jump to Position to the Actions box.
- Set
x
to4
, leavey
as0
, and make sureRelative
is checked. If you were to test this out now, your player will begin moving to the right as his walk cycle animation played. The problem is that now it doesn't stop. - Add another event but this time choose Key Release <Right>. Add Change Sprite to the Actions box but this time select
spr_player_idle
from the menu. This time, if you were to run a test, your player would walk right when holding the right arrow key and go back to his idle pose when it is released.These same steps can be repeated for all directions using corresponding keyboard events but you need to take into consideration whether or not you need to mirror the sprites. If you've created separate sprites for each direction you'll simply choose which sprite you'll need at the time. If, however, you've opted to mirror the sprites, you'll need to add one more thing to each key event.
- Under the Main1 tab, drag and drop Transform Sprite into the Actions box for each key event (key release events included) and modify the
xscale
(for horizontal mirroring) or theyscale
(for vertical flipping). Setting the value to -1 would flip the image and setting it to 1 would revert it to its original facing. If the image is already facing that way, no change is made. Avoid using the options in theMirror
menu, as these selections alter the image from its present state instead of assigning a value. This would mean that if you hit the left arrow key, and the character was already facing left, he would flip and face right. That would be confusing!
Tip
This and any other drag and drop function in GameMaker can also be used in code and scripts. In fact, coding them yourself often allows for greater control over how they work.
How it works...
The movement discussed in this section is quite simple. In this case, you've instructed GameMaker to make your character move left or right and play the walk animation (facing the proper direction) as he goes. Using jump to position allows you to move a character to any point in the screen or, as you did here, move the character relative to its current position, adding the entered values to existing coordinates. Using a negative value would subtract from the current coordinates, causing the character to move in the opposite direction. If you want your character to move up or down you would change the value of y
and leave x
as 0
. I encourage you to play around with the values entered, as this will change the player's speed.
There's more...
It might seem like this section had a lot of text to set up very simple movement, but I can assure you it is all necessary. This section and the rest of the chapter set up some core ideas that will recur throughout your GameMaker experience and will be explored much further later on.
See also
Several methods of advanced player movement and controls will be demonstrated in Chapter 2, It's Under Control – Exploring Various Control Schemes, and Chapter 3, Let's Move It – Advanced Movement and Layout.