Game Development Reference
In-Depth Information
How it works...
The
BetterCharacterControl
class of jMonkeyEngine already has a lot of the func-
tionalities to handle the movement of a character. By extending it, we get access to it and
we can implement the additional functionality on top of it.
The reason we use Booleans to control movement is that the events in
onAction
and
onAnalog
are not fired continuously; they are fired only when they're changed. So, press-
ing a key wouldn't generate more than two actions, one on pressing it and one on releasing
it. With the Boolean, we ensure that the action will keep getting performed until the player
releases the key.
This method waits for an action to happen, and depending on the binding parameter, it will
set or unset one of our Booleans. By listening for actions rather than inputs (the actual key
strokes), we can reuse this class for
non-player characters
(
NPCs
).
We can't handle looking up and down in the same way as we perform sideways rotations.
The reason is that the latter changes the actual direction of the movement. When looking up
or down, we just want the camera to look that way. The character is usually locked to the
ground (it would be different in a flight simulator, though!).
As we can see, the
BetterCharacterControl
class already has ways to handle jump-
ing and ducking. Nice!