Game Development Reference
In-Depth Information
}
private void moveSelectedSprite (float x, float y) {
testSprites[selectedSprite].translate(x, y);
}
The new code adds two new methods, handleDebugInput() and
moveSelectedSprite() to the class. The handleDebugInput() method is added as
the topmost call to ensure that the available user inputs are handled first before other
update logic is executed. Otherwise, similar to the order of updating and rendering
in the game loop, it might introduce some sort of lagging behind the user input and
response to such an event. This method also takes the delta time as an argument.
It is used for the same purpose as it is used in updateTestObjects() —to apply
incremental updates in relation to the time that has passed since the last frame was
rendered. As a measure of precaution, the handling of our debug controls is skipped
if the game is not run on a system that is identified as desktop by LibGDX. In this
way, if we were to only target Android for our game, we could leave all the code for
the debug controls in the game without having to worry about it any time later.
The Gdx.input module provides an isKeyPressed() method that can be used
to find out whether a key is (still) pressed. You have to use LibGDX's Keys class
for valid constants to receive the correct results. So what we basically did here
is ask whether any possible combination of the keys A , D , W , and S is currently
pressed. If a condition returns true , meaning that the key is really pressed,
moveSelectedSprite() is called. The method requires two values that indicate the
direction and magnitude of the desired motion that is to be applied to the selected
sprite. The magnitude here is sprMoveSpeed with a constant value of 5 meters
multiplied by the delta time, which means that our sprite will effectively be able to
move at a speed of 5 meters per second.
You can now start the game at the desktop and try it out. Press any of the keys
( A , D , W , or S ) to move around the selected sprite in the game world.
The next controls to implement are the keys to reset the game world and to select the
next sprite. Once again, add another line of code to WorldController to import a
new class, as follows:
import com.badlogic.gdx.InputAdapter;
 
Search WWH ::




Custom Search