Game Development Reference
In-Depth Information
How to do it...
Perform the following set of steps to create a reusable character control:
1. Start by creating a new class called GameCharacterControl , which extends
BetterCharacterControl . This class also needs to implement Ac-
tionListener and AnalogListener . The idea here is to feed this class with
actions that it can handle. To control the movement of a character, use a series of
Booleans as follows:
boolean forward, backward, leftRotate, rightRotate,
leftStrafe, rightStrafe;
2. Also, define a float field called moveSpeed , which will help you control how
much the character will move in each update.
The control Booleans you added are set in the implemented onAction method.
Note that a key will always trigger !isPressed when released (note that a key al-
ways triggers isPressed == false when released):
public void onAction(String action, boolean isPressed,
float tpf) {
if (action.equals("StrafeLeft")) {
leftStrafe = isPressed;
} else if (action.equals("StrafeRight")) {
rightStrafe = isPressed;
} else if (action.equals("MoveForward")) {
forward = isPressed;
} else if (action.equals("MoveBackward")) {
backward = isPressed;
} else if (action.equals("Jump")) {
jump();
} else if (action.equals("Duck")) {
setDucked(isPressed);
Search WWH ::




Custom Search