Login | Register

Behind TwoLined Movement

January 6, 2008, 22:57 by Schyler
[loading]
-->
GML Coding Tutorial (Topdown Movement)

In this tutorial I will be explaining how you can use a simple (but efficient) few lines of code to handle the movement of topdown objects in a game. I will also be explaining how you CAN alter the scripts to be used in a platformer game, shooter or alter the speed.

To get the simple things out of the way and move onto the discussion, here is the code:

x -= (keyboard_check(vk_left)-keyboard_check(vk_right))*mspeed;
y -= (keyboard_check(vk_up)-keyboard_check(vk_down))*mspeed;

What is happening here is simple. In the create event of the object you set the variable ‘mspeed’ which is short for ‘move speed’. Then you place this code in the Step or Begin Step event of the object you want to move. When the code is executed there is one main calculation: ‘(keyboard_check(key1) – keyboard_check(key2))’. This will return a value either ‘1’ or ‘-1’ depending on which keys are pressed. If both keys are pressed ‘a – b’, therefore, ‘1 – 1’ = 0, so no movement is created.

On the second line the theory is the same; it calculates which keys are being presses etc, however, I will now explain to you how the ‘mspeed’ variable works. Any number, below zero, or above zero, can be multiplied in game maker (unless its larger than 32000). If the expression returns -1 then we multiply it by ‘mspeed’ which could be 4, -1 multiplied by 4 is equal to -4. It’s the same if it returns a positive number. For example, 1 multiplied by 4 is equal to 4.

When you get this value (either zero, negative motion, or positive motion) you subtract it from the position value of the object, which is x or y, therefore making the object move.

Lets consider a short example. The expression returns 4 for the ‘y’ motion. The now has added 4 pixels to its ‘y’ variable making it move downwards. Consider if that motions was -4. It has the opposite effect, making the object move upwards on the screen.

There are other types of games where you can use this snippet of code. Ina platformer you could use:

var tt;
tt = (keyboard_check(vk_left)-keyboard_check(vk_right))*mspeed;
if (place_free(tt,y))
{
x -= tt;
}

Where we don’t modify the ‘y’ value, rather only the ‘x’ value (and let the gravity of the platformer modify the ‘y’ value itself. This code can also be turned into a scrpt so that multiple objects can use it, or so, it can be setup in a two player game to use multiple controls.

x -= (keyboard_check(argument0)-keyboard_check(argument1))*argument4;
y -= (keyboard_check(argument2)-keyboard_check(argument3))*argument4;

Here, arguments 0,1,2 and 3 are the keys that are used for the movement (they are in the order left, right and up, down). The fourth argument is the speed of the movement (rather than setting it in the create event, using a value directly).

This just about concludes this tutorial. Do some homework and find other uses for this script. Find out how this script can modify forces and motion rather than just modifying the ‘x’ and ‘y’ values directly.

Thank you for reading this tutorial.
Please provide feedback.

Comments

Loading comments... [loading]
.
Users logged in:

game maker articles, game maker examples, game maker tutorials, gmtutorials, game maker questions and answers, game maker crash course, how to create games