Game Development Reference
In-Depth Information
Try running the game again and see the result when you jump against the same wall as
shown in Figure 4-3. It works, but it looks quite unnatural, so we're going to impose some limits
on how much of the horizontal speed can be retained after a collision.
2. Use the Define Constants option on the Resources menu (or a new variable in the
Create event of obj_zool if you are using Game Maker Lite) to define a new constant
called MAX_DRIFT_SPEED , which is set to 8 .
3. Now go back to the Speed Horizontal action in the Collision event of obj_zool_air and
change the Hor. Speed setting from hspeed to median(-MAX_DRIFT_SPEED, hspeed,
MAX_DRIFT_SPEED) . Just like when we used this function in the Step event of obj_zool ,
this will limit the size of hspeed to within plus or minus MAX_DRIFT_SPEED (+8 to -8).
Now try playing the game again. If you jump at the same wall again, it should be a little more
believable now. Also try standing right next to the wall at the bottom and making a horizontal
jump to the right. You'll find you land neatly on the top edge of the platform, which is very
helpful! If you play around with it for long enough, you will also find a small bug. If you fall
against one of the walls, you will slide down to the bottom and get stuck! This is because Zool's
horizontal speed keeps him colliding with the wall so he never reaches the bottom. This is a side
effect of our little tweak, which never resets the horizontal speed. This bug will actually solve itself
as we add more of Zool's movement mechanics, but if you're applying this technique to your own
games, then it might be easier to stick with the “basic” form of sliding (setting Hor. Speed to 0 )
and relying on the drifting mechanic described in the next section to get the player over the edge
of the platform.
A Floating Garage Worker
So we're going to add the drifting mechanic next, which gives the player a limited amount of
control over Zool's horizontal movement in the air. We will only allow Zool to slowly change his
speed in the air (by 1 pixel per step), and we won't allow drifting to increase his air speed beyond
MAX_DRIFT_SPEED .
Adding a Drifting Mechanic to the Zool Air State Object
1.
Add a new Keyboard, Left event to the obj_zool_air object and include a Set Variable
action ( control tab) that sets the Variable facing to the Value FACE_LEFT .
2.
Include a Test Variable action that checks if the Variable hspeed is larger than
-MAX_DRIFT_SPEED . So, in other words, it checks that Zool is not already moving left at a
speed that is greater than the maximum drifting speed (negative means left).
3.
Follow this with a Speed Horizontal action ( move tab) with Hor. Speed set Relative to
-1 to increase his speed to the left by 1 pixel per step.
Add a new Keyboard, Right event to the obj_zool_air object and include a Set
Variable action ( control tab) that sets the Variable
4.
f
acing to the Value
F
ACE_RIGHT .
Include a Test Variable action that checks if the Variable hspeed is smaller than
MAX_DRIFT_SPEED . Similarly, this checks that Zool is not already moving right at a speed
that is greater than the maximum drifting speed.
5.
6.
Follow this with a Speed Horizontal action ( move tab) with Hor. Speed set Relative to
1 to increase his speed to the right by 1 pixel per step.
 
Search WWH ::




Custom Search