Learn how to create a player object, assign a sprite, and implement WASD movement controls in GameMaker. This guide is perfect for beginners looking to get started with game development.
Creating the Player Object
In GameMaker, the first step is to create a new object asset that will represent our player. Objects are the core building blocks of your game, containing the code and events that define how they behave.
- To begin, navigate to the Assets panel. This is where you manage all the components of your game, such as sprites, objects, and rooms.
- Click the plus sign to add a new asset to your project. From the list of asset types, choose Object.
- After creating the object, it’s a good practice to give it a descriptive name. The video suggests renaming it to
OBJ_player. Using a consistent naming convention likeOBJ_for all your objects helps keep your project organized. - Finally, for better file management, drag the newly created
OBJ_playerinto your designatedobjectsfolder within the Assets panel.
Assigning a Sprite
The sprite is the visual representation of your object. While the object handles the code and logic, the sprite is what the player will actually see on the screen.
- Select the
OBJ_playerobject. In the Inspector panel, you’ll see a section for the sprite. - Click on the “No sprite” button to open the sprite selection window.
- Browse to your sprites folder and choose the appropriate sprite for your player. For this example, the video uses
sprite_player_idle_down.
Initializing Variables with the Create Event
The Create event is a special event in GameMaker that runs only once, the moment an object is created in the game room. It’s the ideal place to set up initial variables and configurations.
- In the Workspace area for your player object, click the Add Event button and select Create.
- Inside the code editor for this event, you will define two important variables. The first is for the player’s movement speed. You can set this to
move_speed = 1;[03:38]. - The second variable is a reference to the tile map layer, which is crucial for handling collisions later on. The code for this is
tile_map = layer_tilemap_get_id("Tiles");[04:01]. This line gets the ID of the tile map layer named “Tiles,” which you will use to prevent the player from moving through walls.
Implementing Movement Controls with the Step Event
The Step event is a key event that runs every single frame of the game, making it the perfect place to handle continuous actions like player movement.
- To add this event, click the plus sign next to the Create event, then navigate to Step and select Step again.
- Inside the code editor for the Step event, you will write the logic for player input.
- First, check for horizontal input using the ‘A’ and ‘D’ keys. The video uses the following line of code:
var _hor = keyboard_check(ord("D")) - keyboard_check(ord("A"));[05:34]. This code returns a value of1if the ‘D’ key is pressed,-1if the ‘A’ key is pressed, and0if neither or both are pressed. - Similarly, check for vertical input with the ‘W’ and ‘S’ keys. The corresponding code is
var _ver = keyboard_check(ord("S")) - keyboard_check(ord("W"));[07:06]. This provides a1for ‘S’,-1for ‘W’, and0otherwise. - Finally, to make the player move and collide with the tile map, use the built-in
move_and_collidefunction. The code is:move_and_collide(_hor * move_speed, _ver * move_speed, tile_map);[07:33]. This function takes your horizontal and vertical movement values, multiplies them by the speed, and moves the player while automatically checking for and reacting to collisions with the specifiedtile_map.
Placing the Player in the Game Room
With the object created and its behavior coded, the last step is to add it to the game world so you can see it and interact with it.
- Open your game room from the Assets panel.
- In the Room Editor, make sure the Instances layer is selected. This is the layer where all the active game objects reside.
- Drag the
OBJ_playerobject from the Assets panel and drop it into the room at your desired starting location.
Running and Testing the Game
- To test your work, press F5 to compile and run the game.
- Once the game window appears, use the W, A, S, and D keys to test your player’s movement and confirm that the controls are working correctly.
Congratulations! You have successfully created and configured a player object with basic movement controls. The next step in your game development journey would be to implement a camera that follows the player, keeping them in the center of the screen as they explore the game world.
A Step-by-Step Guide to Creating a Player Object in GameMaker was originally found on Access 2 Learn