Home > Tutorials > Game Maker > Basic GML: Movement
Basic GML: Movement 
Basic GML Functions: Movement
-------------------------------------
This is a short tutorial for those who want to learn the basics of Game Maker Language, the scripting language used
Gamemaker. This tutorial is intended for Gamekmaker 7.0 Lite or Pro. It won't teach much, but rather describe some basic
things you can do with GML. This tutorial will look at one of the more used commands in Gamemaker: Movement.
This tutorial will use the following functions:
if- Used to check if the following line is true. You can add =false after to check if it is false.
keyoard_check- This checks whether a key is being pressed.
vk_up/down/left/right/nokey- This references to the directional keys, and can also be used for nokey or anykey.
sprite_index- This changes the object's sprite to the specified sprite.
sound_play()- Plays the specified sound
sound_stop()- Stops the specified sound
The most obvious way to make an object move is using object actions. But that leads to several unnecessary events.
You can cut that down to one by using the below script:
Code:
{
if (keyboard_check(vk_left)) x -= #;
if (keyboard_check(vk_right)) x += #;
if (keyboard_check(vk_up)) y -= #;
if (keyboard_check(vk_down)) y += #;
}
This will check to see if Left, Right, Up, or Down is being pressed, and take a certain action(specified by you).
Simply replace the # signs with numbers. It can be any number you want.I would suggest using 3-5, depending on the
game. If you were, for instance, making a 1942 like game(overhead plane shooter), you could set it as follows:
Code:
{
if (keyboard_check(vk_left)) x -= 3;
if (keyboard_check(vk_right)) x += 3;
if (keyboard_check(vk_up)) y -= 4;
if (keyboard_check(vk_down)) y += 2;
}
This would make moving forward the fastest, while sideways is slightly slower. Moving backwards would act more like
slowing down. Now, what if you want a different sprite for each direction? Not much harder. Simply use the code:
Where * is the name of the sprite you wish to use. In this case, let's say you had animations of the plane tilting right
or left, and wanted them implemented. Simply change your code to this:
Code:
{
if (keyboard_check(vk_left)) x -= 3; sprite_index=*
if (keyboard_check(vk_right)) x += 3; sprite_index=*
if (keyboard_check(vk_up)) y -= 4;
if (keyboard_check(vk_down)) y += 2;
}
Once more, replace * with the required information. In this case, that would be the exact name of the sprite you
want to use, and have imported. This will, when the right or left directional key is pressed, change the sprite into the
specified sprite, as well as move in the specified direction. If you want an idle sprite, or would like it to revert back
to the normal sprite when no key is pressed, use:
Code:
if (keyboard_check(vk_nokey)) sprite_index=*
Here's an image showing how your object's actions will look:

The <Any Key> action will use the same action(Execute Code, located in the Control tab), and will run the above script.
This will cause your object to change sprites when moving left or right, and change to a default or idle sprite when no keys
are being pressed. YOu can have animations for moving up or down as well, obviously. If you wanted to play a sound while
moving, you can use the:
Simply add it to the code, just like you did for changing the sprite. Be warned this will play the entire sound clip, even
if you let go of the key. So if you set it to play a 5 second audio clip, it will play that entire 5 second clip, regardless
of whether you let go of the key or not. To amend this, you can add:
to the code executed in the <No Key> event in your object.This will cause the sound to stop when no keys are pressed.
END TUTORIAL.
Hopefully, this proves useful to those just starting out. Oh, and there is absolutely NO CREDIT NEEDED. This isn't "my script" or anything. It doesn't really do anything new, but rather shows you how to effectively do something.
|
|
Details
|
|
Tutorial:
|
Basic GML: Movement |
|
Date Listed:
|
2008-06-08 |
|
Author:
|
Rast
|
|
Total Hits:
|
5966 |
|
|