Creating 2D movement
In Chapter 1, Game Plan – Creating Basic Gameplay, we demonstrated the basic character movement using GameMaker's drag and drop interface. It involved a lot of blocks. Next, we will create more sophisticated movement code in only a handful of code blocks.
Getting ready
You'll need a character object (obj_player
) and a room where you can place it. We won't be discussing the object's sprite or animation here; this is purely about moving your character in two dimensions on the screen. For this reason, we're using a simple block to represent our player character.
How to do it...
- In
obj_player
, add a Create event. - Drag and drop an Execute Code block (under the Control tab) into the Actions box.
- Open it and enter the following code:
///set variables max_spd = 12; accel_spd = 1; decel_spd = 2;
- Close this code block and add a Step event.
- Drag a code block to the Actions box and enter the following code:
///control movement if (keyboard_check(vk_right)...