Movement in Game Maker
Alright, so this is my first tutorial with the TAW program. It's not going to be organized the same way as the VX ones. It's more of an actual tutorial and less of a downloadable demo (although there is one at the bottom of the post).
Link back to TAW program: http://www.rpgrevolution.com/forums/index....showtopic=31232
This tutorial is covering on-foot movement, not vehicle movement. I don't want to go into things like friction and intertia now. When you think about it, there are several fundamental types of character movement control for games. I will only be going into "direct movement" in this tutorial (my name, not an official name for it).
I am not dealing with the following in this tutorial:
-Rotate and Move
-Point and click
-Any other I can't think of (duh)Direct Movement
This is the simplest type of movement around. When the right arrow is pressed, the character moves towards the right side of the screen. Up moves toward the top of the screen, etc. Basically, the logic will flow like so:
is the player pressing left?
If so, move left
is the player pressing right?
If so, move right
is the player pressing up?
If so, move up
is the player pressing down?
If so, move down
Notice I did not include ELSE statements. This movement will be based on pixels and not based on speed. This means that if up and down are both pressed they will automatically cancel eachother out (which is awesome). It also means that diagonal movement is very easy. However, it's not "real" diagonal movement. It just adds the vertical and horizontal pixel movement, it doesn't use any math to make it work realistically. Many games do this, and I feel that it's an acceptable break from reality. It certainly makes collision checks easier.
Anyway, let's open up Game Maker. I use Game Maker 7, because it's the most recent release, and the only one that works on Vista. It has some bugs on Vista though. The names of the tabs don't appear in the object editor, for example.
If you are running Vista DO NOT run the program in admin mode. It will be impossible to test and compile your projects. When you start up GM in Vista, you might get this message:
Just click the OK button and continue. It should work fine anyway.
Anyway, I'm going to assume you know how to create sprites and objects. I've created a basic object here using one of the sprites I've made for a portrait for my game:
I really recommend turning precice collision checking off for this type of movement. You don't want to get the character stuck in the wall because his sprite animates.
Easy so far. I'm going to assume you know how to add events to objects, and then add actions to them, so I'll just explain this with text.
One more object to make before we do anything else. Make a new sprite that's a solid colored square, and turn transparency off. Make an object for it, and set it to solid. This will be the "wall" for collision detection.
Back to the character object:
You're going to be making a "keyboard" event for each arrow key - left, right, up, and down. I recommend making one and then duplicating and editing it for the other directions. It's just a little time saver.
Anyway. Make the first event. I recommend starting with right. Now that you have the event added, let's add some actions.
In the first tab on the action list, find "jump to position." It should be the 4th one down in the leftmost column.
Right click on it or left click and drag it into the Actions column. This will open up a dialog asking you to define some stuff. If you're starting with "right" set x to 4, keep y 0, and check the "relative" box. The "relative" box means that it will move based on its current position, not based on the top-left corner of the room. An alternative would be to write "x+4" in the box instead. If you do it that way, don't check "relative."
You've now told the object that whenever the player presses the right arrow, the object should move 4 pixels to the right. But hang on, what about those walls we added? Well, we'll need to make the object check whether there's a wall in the way before deciding whether to move or not. This is fairly easy with drag and drop actions (for some reason, I found it more difficult to do with scripting).
Anyway, here's what your going to do. First, go to the "control" tab (4th tab) in the actions list. Right click on or drag "Check empty" into the actions box. This will open up a dialog. Use x = 1, y = 0, relative. Keep it set to "only solid." This basically checks if the position is empty before it moves there.
Drag the action you just created to the very top of the list of actions for the event. You want it to be checking before it actually moves. Now you need to make it a bit easier for the system to read. See the two triangles in the actions list? drag the right-side-up one just below the "check empty" action. Drag the upside-down one to below the "jump to position." This isn't necessary if you only have one action after the check, but if you want multiple actions to only occur if the check returns true, you need these triangles to enclose the code.
Duplicate the whole set of code you made for each of the arrow keys, by right clicking on the Event and selecting Duplicate Event. You will need to change some values:
for Left: x = -4, y = 0
for Up: x = 0, y = -4
for Down: x = 0, y = 4
Use 1 and -1 for the collision checks.
That should be everything! If you want a neat effect, go into the sprite editor and change the "top" value of the character's bounding box. If you make the value larger, he will be able to overlap walls a bit. This doesn't look good with all art styles, though.
Here's a demo:http://www.mediafire.com/download.php?k2gokizcnd4
I forgot to mention this in the tutorial, and I didn't do it in the demo, but:
The character's sprite's bounding box size should be a multiple of the move speed (4 in this case). If you don't then the character won't be able to go flush to the wall on all sides.
EDIT: Fixed a problem I found with the collision detection. Assuming everything is based on a grid, you should only use 1 and -1 for the check collisions.