Game Maker Tutorials: Basic Sprite Movement in GML

Posted by on Aug 20, 2011 in Game Maker, Tutorials | 1 comment

I’ve been messing around with Game Maker Pro version for a couple of months now, and I learned a few tricks to help you build your own game. Game Maker is a software that you can use to create your own game without any or little knowledge in coding Java, C++, etc. By the way, Game Maker has a free version “Game Maker Lite” which you can download at the official site yoyogames.com. What I’m going to teach today is about basic sprite movement, it can be 4-directional and 8 directional as you like.

So let’s start:

First you will need to create a sprite, a graphics object. To do that, check the toolbar at the upper portion of the software and look for Resources > Create Sprite, you can use Shift+Ctrl+S for shortcut. Now, there will be a few buttons there. Just for the sake of speeding up this tutorial, click “Load Sprite” to use any image you have available. If you don’t have any, Google “sprites”.

Next, create an object in Resources > Create Object or Shift+Ctrl+O and Load your sprite.

Then Click the button “Add Event” and then click “Create”, the one with the lightbulb icon. Look at the right side of the popup, find the “Control” tab and click it. Find the “Execute Code” icon and drag it in the actions area.

Place this variable:

movement = 0;
// 0 = stop ; 1 = right ; 2 = up ; 3 = left ; 4 = down ; 5 = upright ; 6 = downright ; 7 = downleft ; 8 upleft

And click the check icon. Now add another event, this time we will use “Step” Event. Drag the “Execute Code” again and place this.

// movements
// 0 = stop ; 1 = right ; 2 = up ; 3 = left ; 4 = down ; 5 = upright ; 6 = downright ; 7 = downleft ; 8 upleft

// 4-direction
// upward movement
if (keyboard_check_direct(vk_up))
{
movement = 2;
}
if (keyboard_check_released(vk_up))
{
movement = 0;
}

// downward movement
if (keyboard_check_direct(vk_down))
{
movement = 4;
}
if (keyboard_check_released(vk_down))
{
movement = 0;
}

// leftward movement
if (keyboard_check_direct(vk_left))
{
movement = 3;
}
if (keyboard_check_released(vk_left))
{
movement = 0;
}

// rightward movement
if (keyboard_check_direct(vk_right))
{
movement = 1;
}
if (keyboard_check_released(vk_right))
{
movement = 0;
}

//diagonal movements

if (keyboard_check_direct(vk_up) and keyboard_check_direct(vk_right))
{
movement = 5;
}
if (keyboard_check_released(vk_up) and keyboard_check_released(vk_right))
{
movement = 0;
}

if (keyboard_check_direct(vk_down) and keyboard_check_direct(vk_right))
{
movement = 6;
}
if (keyboard_check_released(vk_down) and keyboard_check_released(vk_right))
{
movement = 0;
}

if (keyboard_check_direct(vk_up) and keyboard_check_direct(vk_left))
{
movement = 8;
}
if (keyboard_check_released(vk_up) and keyboard_check_released(vk_left))
{
movement = 0;
}

if (keyboard_check_direct(vk_down) and keyboard_check_direct(vk_left))
{
movement = 7;
}
if (keyboard_check_released(vk_down) and keyboard_check_released(vk_left))
{
movement = 0;
}

//actual movement

if (movement = 0) // stop
{
// do nothing
}
if (movement = 1) // right
{
x = x+4;
}
if (movement = 2) // up
{
y = y-4;
}
if (movement = 3) // left
{
x = x-4;
}
if (movement = 4) // down
{
y = y+4;
}
if (movement = 5) // upright
{
y = y-4;
x = x+4;
}
if (movement = 6) // downright
{
y = y+4;
x = x+4;
}
if (movement = 7) // downleft
{
y = y+4;
x = x-4;
}
if (movement = 8) // upleft
{
y = y-4;
x = x-4;
}

Now, press F5 to test it. Move your character by using the arrow keys.
If you have any question, send it through the comment section below.

No related posts.

1 Comment

  1. Great, I am new to Game Maker and I’m gonna try this out.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>