Gw Temp

Menu

Tutorial - '8 Way Directional Movement' by Jag

An item about RPGMaker 2000 posted on

Blurb

How to make your game use 8 way movement rather than the traditional 4 way.

Body

First off these are the skills you will need:

At least an amateur knowledge of GM overall
A slight knowledge of GML

There see, it's not to hard :)

You should already know what an event is, but I'll go over it anyway.
When you create an object, it has it's own set of "events".
Events are times when something is happening, like if a key is pressed, or there is a collision.
For this we will work only with the keyboard events.

The first thing you will want to do is create a new script, and put the following code in:

global.playerSpeed = 5;

Save the script as "GameStart".
What does this do?
It sets a global variable (playerSpeed) to 5, we will call this variable later.
We need to create a new object, called "objGameStart", choose any sprite for it, but un check the "visible" box (so it is invisible).
Then goto it's "Game Start" event (click on the drop down for the event with the little blue ?), goto the "Code" tab and drag "Execute a Script" over to the white area (looks like a piece of paper, with a big green arrow on it.), a new window will open asking you which script to run, select "GameStart" and leave everything else alone, press "Ok" to close the window, then press "Ok" to close the object window.


Now we are going to open up the player object, and go into it's "Left" keyboard event. (Click on the button with a keyboard on it, and then select the drop down until you see "Left" click on that.)
Go over to the "Code" tab, and drag "Execute a Piece of Code" (looks like a little piece of paper) into the white area.
A new window should open, which looks something like notepad.
Type the following code in:

x -= global.playerSpeed;

This will make it so the player moves left.
Notice how instead of adding a number value to the move speed, I called the global variable?
This makes it so if I want to make the player move faster, or slower, I simply change the global variable once, instead of having to change all the pieces of code scattered throughout the player object!
Click the little green check mark to save and close the code window.
Well, we still have 3 more events to make, let's get to it!

Now goto the "Right" keyboard event.
Drag over "Execute a Piece of Code", and type this in:

x += global.playerSpeed;

Now the player can move left, and right!
Notice how I use the same global variable, as I explained, this will make editing easier in the future.
Click the check mark to save and close.

Now goto the "Up" keyboard event, and drag "Execute a Piece of Code".
Type in the following code:

y -= global.playerSpeed;

The player now has 5 directional movement!
Left, Up, Right, Right-Up, and Left-Up!
Just one more event to edit!
Goto the "Down" keyboard event, and add this code:

y += global.playerSpeed;

There you go, 8 directional movement!
The player can move, Left, Right, Up, Down, Left-Up, Left-Down, Right-Up, and Right-Down!

Before I leave you, I'm going to try to explain how this works.

x and y are built-in variables for Game Maker, by setting the x and y variables plus some, or minus some, the object is moved.
And when you set the x AND y variable at the same time, you move diagonally.

Well, there you have it, quick and easy 8-Directional Movement!
Enjoy!