Introducing Dragonfly Cloud! Learn More

Question: How do you make a character move in GameMaker?

Answer

In GameMaker, moving a character typically involves changing their x and y coordinates on the screen. Here's a step-by-step guide to making a basic character movement system:

Step 1: Create the Character Object

  • In your GameMaker project, create a new object (e.g., obj_player).

Step 2: Set Up Movement Variables

  • In the Create Event of obj_player, initialize your movement variables.
// Create Event of obj_player var speed = 4; // The speed at which the character will move.

Step 3: Write the Movement Code

  • To prevent permanent movement when keys are not pressed, ensure motion only occurs during key presses. Add this code in the Step Event of obj_player:
// Step Event of obj_player // Initialize movement direction variables var mov_x = 0; var mov_y = 0; // Determine movement direction based on key presses if (keyboard_check(vk_left)) { mov_x -= 1; } if (keyboard_check(vk_right)) { mov_x += 1; } if (keyboard_check(vk_up)) { mov_y -= 1; } if (keyboard_check(vk_down)) { mov_y += 1; } // Normalize vector if both components are non-zero to maintain consistent speed in all directions if (mov_x != 0 && mov_y != 0) { var len = sqrt(mov_x * mov_x + mov_y * mov_y); mov_x /= len; mov_y /= len; } // Apply movement x += mov_x * speed; y += mov_y * speed;

Step 4: Add Sprites and Collision (Optional)

  • Assign sprites to your obj_player to visually represent the character.
  • If necessary, add collision detection to prevent the character from moving through solid objects.

Step 5: Test Your Game

  • Place obj_player in a room and run your game to test the movement.

This is a very basic example of character movement. Depending on your needs, you might want to implement acceleration, friction, or more complex mechanics like jumping or pathfinding. Also, consider using keyboard_check_pressed for one-time key presses and keyboard_check_released when you want an action to occur after releasing a key.

As your game development progresses, you may want to look into finite state machines (FSM) for more complex character behaviors and better code structure and organization.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.