Game Development Reference
In-Depth Information
about using a separate [cn]repu variable is that it makes the code easier to understand, and it keeps
your options open for mixing and matching the [cn]repu force with other physical forces that you
There are two other bits of fine-tuning done to this code. I want the object to fall at a faster rate than
it ascends. I modified the speed limit in the eb statement that checks to see how fast the object is
moving down the stage:
eb\$[ru:\$[olaa`Heiep&.%%
w
[ru9\$[olaa`Heiep&.%7
pn]_a\$paniej]hrahk_epu%7
y
eb\$[ru8)[olaa`Heiep%
w
[ru9)[olaa`Heiep7
y
Multiplying it by . makes it fall twice as fast as it climbs. It also displays terminal velocity when it reaches
its maximum speed. You could use this in a Lunar Lander-type game to figure out whether the space-
craft is going too fast when it hits the planet surface.
The other small change is that friction isn't added to the [ru propertyâ€”gravity takes care of that
as well.
Jumping
Probably half of all video games ever made use jumping as a primary character action. All it boils down
to is a temporary increase in the object's u velocity. Once you understand how acceleration, friction,
and gravity work, jumping is not at all difficult to implement. However, there are a few additional
things to keep in mind that make it a little more complex:
You want your object to be able to jump when it's on the ground, and only on the ground. But
how will your object know that it's on the ground? And what is the ground, anyway? The code
has to be able to figure these things out.
You need to prevent the jump keys from triggering a jump more than once if they're held
down.
Let's look at an example of jumping in action and how to solve these problems. Bind the Lh]uan[Fqil
class to the ?]p symbol. Test the project. The left and right arrow keys move the cat horizontally, and
pressing the up arrow key or the spacebar makes it jump. While the cat is moving up, its ears flap, but
they stop flapping when it's moving down toward the ground. Figure 9-5 illustrates this.