Game Development Reference
In-Depth Information
The InputAdapter class is a default implementation of the InputProcessor interface
that provides various methods to handle input events. We want to use the adapter
variant instead of the InputProcessor . This is because it is a convenient way of not
being forced to implement all the interface methods when you know that you are
not going to implement most of them anyway. It would be perfectly valid, of course,
to still use the InputProcessor interface since it is just a matter of taste. Derive
WorldController from InputAdapter by changing the existing class like this:
public class WorldController extends InputAdapter {
// ...
}
Then, add the following code snippet to the existing class:
private void init () {
Gdx.input.setInputProcessor(this);
initTestObjects();
}
The WorldController class serves a second purpose from now on by also being an
instance of the InputProcessor interface that can receive input events. LibGDX needs
to be told about where it should send the received input events. This is done by calling
setInputProcessor() from the Gdx.Input module. As WorldController is also our
InputProcessor , we can simply pass it into this method.
Now that LibGDX will send all the input events to our listener, we need to actually
implement an event handler for each event we are interested in. In our case, this will
only be the event where a key was released. These events are handled in keyUp() .
Override the adapter's default implementation of this method with the following code:
@Override
public boolean keyUp (int keycode) {
// Reset game world
if (keycode == Keys.R) {
init();
Gdx.app.debug(TAG, "Game world resetted");
}
// Select next sprite
else if (keycode == Keys.SPACE) {
selectedSprite = (selectedSprite + 1) % testSprites.length;
Gdx.app.debug(TAG, "Sprite #" + selectedSprite + " selected");
}
return false;
}
 
Search WWH ::




Custom Search