Game Development Reference
In-Depth Information
inputManager.addMapping(InputMapping.StrafeLeft.name(),
new KeyTrigger(KeyInput.KEY_A), new
KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addMapping(InputMapping.StrafeRight.name(),
new KeyTrigger(KeyInput.KEY_D), new
KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addMapping(InputMapping.MoveForward.name(),
new KeyTrigger(KeyInput.KEY_W), new
KeyTrigger(KeyInput.KEY_UP));
inputManager.addMapping(InputMapping.MoveBackward.name(),
new KeyTrigger(KeyInput.KEY_S), new
KeyTrigger(KeyInput.KEY_DOWN));
}
Note
It's okay to assign several keys to the same mapping. For example, this recipe as-
signs both the arrow keys and the classical WASD pattern to the movement keys.
5. Finally, in the same method, we tell
InputManager
to listen to the commands,
or it won't actually fire on any of the inputs:
for (InputMapping i : InputMapping.values()) {
inputManager.addListener(this, i.name());
}
6. Now, once
AppState
is attached, it runs the
initialize
method (in a
thread-safe way). Here, we get the reference to the application's
InputMan-
ager
object and run the
addMappings
method we just created, as follows:
public void initialize(AppStateManager stateManager,
Application app) {
super.initialize(stateManager, app);
this.inputManager = app.getInputManager();