Alright, so by now you have probably at least gone through the “Your First
Game” tutorial in Gamemaker.  If not follow this link and find
supplemental information in my previous blog post, Part One.

 

1. Movement

Now, let’s say that you want to control the movement of a character with the
arrow keys instead of having him simply move in random directions as it does in
Gamemaker’s first tutorial.  There are a few basic things we need to know before
proceeding, however.  The first of which is the fact that you can think of your
game room as a grid with X and Y coordinates.  However, contrary to the origin
being in the bottom left corner as you might be used to, our origin is actually
the top left corner.  This means that to move a character to the right, you
simply increase his X coordinate.  Similarly, to move a character down, increase
his Y coordinate.  Here are four simple rules for movement.

1. Moving right- increase X and keep Y

2. Moving left- decrease X and keep Y

3. Moving up- keep X and decrease Y

4. Moving down- keep X and increase Y

Now how to apply these rules?  It’s actually pretty simple.  Assuming  you
have an object already placed in your room, open up your object.  Now add the
following four events; <Left>, <Right>, <Up>, <Down>. 
To do this, click on “Add Event” and then “Keyboard”, and you will see these
four at the top.

 

After adding all four events, you should see this. 

  

Now let’s start with moving right.  We are going to use a “jump to position”
action, and use the logic from rule 1.  So for our X and Y coordinates, we are
going to use 4 and 0 respectively.  Also make sure that “relative” is checked so
that you are jumping to a position relative to your current position instead of
jumping to that specific position on the grid. 

 

Press okay, and you now have movement to the right!  You will do the same for
the other three directions except you will use slightly different coordinates. 
If you follow the four rules from above, you can probably figure them out on
your own, but if not, here they are.

1. right- (4,0)

2. left- (-4,0)

3. down- (0,4)

4. up- (0,-4)

Now save your work and run your game.  You should be able to control your
character in all four directions!  But if you play around with it enough, you
may realize that you can actually move your character right off of the screen. 

 

2. Boundaries

There’s a couple of different ways to solve this.  For now, we are going to
check that the place we want to move to, our future position, is indeed inside
of the room before we actually move.  Again, there’s something important to
understand before we do this.  There are two built in variables that Gamemaker
provides, room_width and room_height, that provide you with…you guessed it, the
width and height of the room respectfully.  So we can use these variables to
test whether our future coordinates our within the room.  For example, if we
want to move right, we want to check that our future X coordinate is less than
room_width.  To do this, we are going to use a “test variable” action.  You can
find it under the “Control” tab, in the “Variables” section.  It will be the
middle octagon looking one, show below.

                                                                                  

When you drag it onto the screen a box will come up.  You want to test
whether your future X coordinate (x+4) is less than room_width before you move,
so fill it out like this…

 

Lastly, you want to make sure that this is the first action that gets
executed, so you should drag this action above the “jump to position” action
that we already had.  For moving right it should end up looking like this.

 

 

Now you need to add this to the other three directions.  Again, you may be
able to figure this out on your own, but if not, here’s some help.

1. right- test that our future X (x+4) is less than room_width

2. left- test that our future X (x-4) is greater than 0

3. up- test that our future Y (y-4) is greater than 0

4. down- test that our future Y (y+4) is less than room_height

After you get these conditions set for your four directions, you should
notice that if you move right, your character will eventually stop thanks to our
boundaries.  You might also notice that, although the character does indeed
stop, part of him might be hanging out off of the screen.  This depends on
several factors like size of your object, its origin, etc.  I won’t get into all
of that, but for now, you can play around with your “future coordinates”.  For
instance, for moving right, you might try (x+60) as your future X.  Like I said,
you can play with these numbers so that they better fit your character and
game.

3. Summary

Hopefully now you have a character that can move in all four directions
without going off of the screen.  There, of course, are other ways to accomplish
this kind of movement, but this one made the most sense to me and hopefully it
does to you as well.  As always, feel free to post any questions or
comments!