Game Development Reference
In-Depth Information
xx$arajp*gau?k`a99Gau^k]n`*OL=?A%%
w
eb$[eoKjCnkqj`%
w
[]__ahan]pekjU9[fqilBkn_a7
[eoKjCnkqj`9b]hoa7
pdeo*a]no*lh]u$%7
y
y
y
If the spacebar or up arrow key is pressed, the code checks to see whether the object is on the
ground. If it is, it adds the jump force value to the vertical acceleration. It also sets the [eoKjCnkqj`
variable to b]hoa. In this example, it also makes the cat's ears flap.
The last little technical issue that you need to solve is that after the preceding code sets
[]__ahan]pekjU to -10, it remains that way until the jump keys are released. That means that if the keys are
held down, []__ahan]pekjU will remain at -10, and the object will keep jumping. To fix this, you
need to give []__ahan]pekjU a value of zero if the object is not on the ground. This section of code in the
kjAjpanBn]ia event handler accomplishes this:
eb$[eoKjCnkqj`%
w
[]__ahan]pekjU9,7
y
One more little detail prevents the cat's ears from flapping when it descends from the apex of the
jump. This is easy to do:
eb$[ru:,%
w
pdeo*a]no*ckpk=j`Opkl$-%7
y
The animation stops if the cat's vertical velocity is less than ,, which means it's moving toward the
bottom of the stage.
Finally, there's no speed limit set if the object ascends because that would conflict with the value of
[fqilBkn_a.
Stage boundaries and subobjects
There's another feature in the Lh]uan[Fqil class that will get you thinking about some of the other
issues you'll be looking at in the example Bug Catcher game, coming up next. If you move the cat to
the left or right side of the stage, its ears extend beyond the stage boundaries. It is, in fact, the cat's
body that's being blocked at the edges.
This happens because the cat has a subobject called ^k`u. It's a round black circle that defines the
main area of the cat. It seemed to me that it looked more natural to use it as the cat's collision area
instead of using its overlong ears. The ears are great for flying, but seem to get in the way of every-
thing else!
 
Search Nedrilad ::




Custom Search