Game Development Reference
In-Depth Information
subtract the max_speed variable from the x value moving us to the left, but immediately
after, we then add our max_speed back to the x value, moving it back to the right. As
this is done in one step, we don't physically see the change, although it does indeed hap-
pen. The reason we don't see the change is because, in this case, the screen is re-drawn at
the end of each step and by that point, both movements have already taken place before
the changes are displayed to the player.
Right now, if we place walls in our room, we will be able to move right through them.
This is because we aren't telling the player what to do when in a collision or interacting
with the walls in anyway.
To make it so, the player can't move through walls, we need to add a collision event with
the wall and type some code to move the object back to its previous position.
In GameMaker, every object has a xprevious and yprevious variable built in. These
variables are always holding the x and y position of the object from the previous step. If
the instance has not moved between steps, then the previous values of x and y will remain
the same.
Add a collision event with obj_Wall , drag in a code block, and then type in the follow-
ing code:
x=xprevious;
y=yprevious;
What this does is when the player object picks up on a collision with a wall, it will set its
x and y position back to its previous position, which is outside of the wall. This makes the
player look like it has stopped when colliding with the wall.
Place wall objects around the border of the entire room and then run the game and try to
move through them. If all has gone well, the player shouldn't be able to get out of the
room.
Search WWH ::




Custom Search