Thursday, April 11, 2013

GML: 8 Direction Movement Tutorial

Update:  Part 2 can be found here.

Now that you have a few basics down, let's actually DO something with them.  Older style top down games allow players to move in eight different directions (ok, sometimes four, shut up, this is MY tutorial).  Now, how do you accomplish that in Game Maker?

Well, first you have to create an object and a room.  You can name them whatever you like.  Oh, you'll need a sprite as well.  It can be a picture of a goldfish.

Place one instance of your object in that room you created.  It doesn't really matter where, but you should probably put it at exactly (249,156).  Once that's done, we can get to coding a bit.

You can build a movement engine like this in a lot of ways, and I'm just going to present one possibility here.  Let's create an empty skeleton for all our code.

Obviously, you're going to want to check your keyboard.  I'm going to use the arrow keys.  This all goes in the step event of your player object:

if (keyboard_check(vk_left)){
    x-=3;
}
if (keyboard_check(vk_right)){
    x+=3;
}
if (keyboard_check(vk_up)){
    y-=3;
}
if (keyboard_check(vk_down)){
    y+=3;
}

Now run your game.  You should be able to run around a blank empty space. 

Edit and use to your heart's content.

Update:  Part 2 can be found here.

No comments:

Post a Comment