Game Development Reference
In-Depth Information
When any of the arrow keys is pressed, these new values are assigned. All you need to do to make
the object move is to assign these values to the object's [rt and [ru properties. You can do this easily
enough with two lines of code:
[rt'9[]__ahan]pekjT7
[ru'9[]__ahan]pekjU7
However, if you leave things as is, the acceleration values are added to the object's velocity on every
frame, without any limit to how fast the object can go. This means that the object eventually moves so
fast that it will be nothing more than a blur on the stage. This won't be of much use in most games,
so it's usually a good idea to assign a speed limit, which is what this section of code does:
eb\$[rt:[olaa`Heiep%
w
[rt9[olaa`Heiep7
y
eb\$[rt8)[olaa`Heiep%
w
[rt9)[olaa`Heiep7
y
[ru'9[]__ahan]pekjU7
eb\$[ru:[olaa`Heiep%
w
[ru9[olaa`Heiep7
y
eb\$[ru8)[olaa`Heiep%
w
[ru9)[olaa`Heiep7
y
The acceleration values are added to the object's velocity only if the [rt and [rt properties are within
the speed limit, which is 1 in this case. This means that the object will accelerate up to 5 pixels per
frame and then travel at a constant rate. The logic behind this is exactly the same logic used to set
stage boundaries.
The next step is to add these new velocity values to the object's t and u positions. That's done with
some venerable old friendsâ€”the same directives you've been using since Chapter 6:
t'9[rt7
u'9[ru7
In fact, these two directives are all you will ever need to move the objects, even though the phys-
ics involved in making them move become quite complex. All the physics calculations are applied
to the [rt and [ru variables; they are then simply assigned to the object's t and u properties to make
the object move.