Introduction to GameMaker Studio 2
Welcome to this guide on creating a player object and implementing basic movement in GameMaker Studio 2. This tutorial is designed for beginners and provides a clear, step-by-step approach to help you understand the core concepts of object creation and coding for player control. By the end of this handout, you’ll have a fully functional player character that can move around your game room.
GameMaker Studio 2 is a powerful and intuitive game development platform that uses a simplified language called GML (GameMaker Language) to make game creation accessible. A key concept in GameMaker is the use of objects and events. Objects are the building blocks of your game, representing everything from your player to enemies and items. Events are what trigger specific actions within these objects, like responding to user input or checking for collisions.
Setting Up Your Player Object
The first step in bringing your character to life is creating a player object. This object will house all the code and attributes that define your character’s behavior. In GameMaker Studio 2, you’ll find the “Objects” folder in your Asset Browser.
To create a new object, right-click on the “Objects” folder and select “Create” and then “Object”. Name your new object “OBJ_player” to keep your project organized. It’s a good practice to use a naming convention like “OBJ_” for all your objects to easily identify them later.
Once the object is created, you need to give it a visual representation. This is done by assigning a sprite to the object. In the object’s properties window, there is an option to assign a sprite. Click on this and select “sprite_player_idle_down” or any other suitable sprite you’ve imported for your player.
Coding for Movement
Now that you have your object, you need to program its movement. This is where you’ll use events and GML code.
First, add a Create Event to your OBJ_player object. The Create Event runs only once when the object is created in the game. This is the perfect place to initialize variables. Inside this event, you will define the player’s movement speed and assign the tilemap layer for collision detection.
move_speed = 3;
tile_map = layer_tilemap_get_id("Tilemap_layer");
The next crucial event is the Step Event. The Step Event runs every single frame, making it ideal for continuous actions like checking for player input and updating movement. Add a Step Event to your object.
Inside the Step Event, you’ll create two variables, ho and ve, to handle horizontal and vertical input. The ho variable will check if the user is pressing the ‘A’ or ‘D’ keys, and the ve variable will check for the ‘W’ or ‘S’ keys.
var horz = keyboard_check(ord("D")) - keyboard_check(ord("A"));
var vert = keyboard_check(ord("S")) - keyboard_check(ord("W"));
This simple line of code checks for key presses and assigns a value of 1, -1, or 0, which is perfect for controlling direction.
Next, you will use the move_and_collide function to handle the actual movement and collision detection with the tilemap. This function simplifies movement by automatically handling collisions.
if (horz != 0 or vert != 0) {
move_and_collide(horz * move_speed, vert * move_speed, tile_map, true);
}
This code snippet checks if there is any movement input (ho or ve is not 0) and, if so, moves the player by multiplying the direction variable (ho or ve) with the move_speed variable you defined earlier.
Adding Your Player to the Game
With your object and code complete, the final step is to place the player in your game room. The game room is the scene where all your game’s assets and objects are placed.
Find the “OBJ_player” object you created in the Asset Browser. Drag and drop it onto the “instances” layer within your game room. This will create an instance of your player object in the game.
To test your work, simply run the game. You should now be able to control your player character using the ‘W’, ‘A’, ‘S’, and ‘D’ keys and navigate the game room. This is a foundational skill in game development and a significant first step in creating your own game.
GameMaker Studio 2: Creating a Player Object and Basic Movement was originally found on Access 2 Learn