360° Movement

One of the first things you want to do when you start using Game Maker might be making a GTA clone or at least something similar.
Anyways, I'm going to show to how to make a simple top-view movment engine now. There's a million ways to do this, and I belive this is one of the easiest ways to do it.

Open Game Maker and create a new sprite like this:

Game Maker Sprite

You can use any kind of sprite for this, but it will of course look best if you choose one that is viewed from the top.
Make sure you apply the same settings as I did, except from the origin if you sprite is bigger or smaller then the one I used. So just press center, and you're good to go.
Whene we're going to rotate the sprite later, it will rotate around the origin, so to make it smooth, we have to make sure the origin is at the center of the sprite.

Now, create a new object, apply the sprite you just created and add a step event like this:

Game Maker Object

As you can see, I alse added a script to the step event. Go ahead and to the same.
Open the script and enter this code:

Game Maker Code

( You learn more if you acctually type it down yourself and not just copy&pasting it :P )
Anyways, let me break it down for you.
As you all should know, Newtons first law tells us that if an object aint affected by any forces, it will either have no speed or constant speed.
So we need to add some kind of force to make the player stop when we don't want it to move.
So let's go with some friction!
Game Maker has its own buildt in friction that slows the object down with x pixels per step.
We used friction = 0.2; so every step the players speed will decrease with 0.2.

Next, we have the controls, let's start with the turning controls:

if (keyboard_check(vk_left)) { direction += 5; } if (keyboard_check(vk_right)) { direction -= 5; }

I'd like to belive that this explains itself, but just to be sure:
keyboard_check returns true with the key "vk_left" (the left arrow key) is pressed. (If you want to use a letter insted, like "A" for example, you have to do it like this: keyboard_check(ord("A"));
direction +=5; adds 5 degrees to the players direction, thus, making it turn.
I'm sure you can figure out the right turing now.

Now, over to the second last step:

if (keyboard_check(vk_up)){ speed = 3; } else if (keyboard_check(vk_down)){ speed = -1; }

This code checks if the up key is pressed, if it is, the players speed will be set to 3 (will move 3 pixels per step).
If the down key is pressed the player will move backwards with 1 pixel per step.

And to acctually rotate the player we put image_angle = direction;
Wich sets the angle of the sprite to the constantly updating direction.

Well, I hope you learned something from this.
Have Fun.

Download .gm6

Want to get in touch? Got some feedback for me? or a couple of requests? Follow me at twitter:

Need a reliable webhost? Take my word for it and go with Hostgator Green Web Hosting by HostGator

Warning: fread(): Length parameter must be greater than 0 in /customers/f/2/7/n1studios.net/httpd.www/minicounter.php on line 17