Game Development Reference
In-Depth Information
There are two ways to handle the input events. We will make use of both of them
shortly to demonstrate when and how to use them. The debug controls we are going
to implement will allow us to do the following operations:
• Move a selected sprite into any of the four directions (left, right, up, or down)
• Reset the game world to its initial state
• Cycle through the list of sprites to select the other ones
The first of the three requirements is quite different to the other two in respect of
continuity. For example, when holding down a key for a move action, you would
expect this action to be repeatedly executed while the key is still being pressed.
In contrast, the other two actions are characterized by being nonrecurring events.
This is because you usually don't want to reset the game or cycle through the list of
sprites a hundred times per second when the respective key is pressed and even held
for a longer period of time.
Let's begin with the movement of a selected sprite that uses the continuous execution
approach. Add the following line of code to WorldController to import a new class
that holds all the available key constants that are supported by LibGDX:
import com.badlogic.gdx.Input.Keys;
Then, add the following code:
public void update (float deltaTime) {
handleDebugInput(deltaTime);
updateTestObjects(deltaTime);
}
private void handleDebugInput (float deltaTime) {
if (Gdx.app.getType() != ApplicationType.Desktop) return;
// Selected Sprite Controls
float sprMoveSpeed = 5 * deltaTime;
if (Gdx.input.isKeyPressed(Keys.A)) moveSelectedSprite(
-sprMoveSpeed, 0);
if (Gdx.input.isKeyPressed(Keys.D))
moveSelectedSprite(sprMoveSpeed, 0);
if (Gdx.input.isKeyPressed(Keys.W)) moveSelectedSprite(0,
sprMoveSpeed);
if (Gdx.input.isKeyPressed(Keys.S)) moveSelectedSprite(0,
-sprMoveSpeed);
 
Search WWH ::




Custom Search