Game Development Reference
In-Depth Information
// Camera Controls (zoom)
float camZoomSpeed = 1 * deltaTime;
float camZoomSpeedAccelerationFactor = 5;
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) camZoomSpeed *=
camZoomSpeedAccelerationFactor;
if (Gdx.input.isKeyPressed(Keys.COMMA))
cameraHelper.addZoom(camZoomSpeed);
if (Gdx.input.isKeyPressed(Keys.PERIOD)) cameraHelper.addZoom(
-camZoomSpeed);
if (Gdx.input.isKeyPressed(Keys.SLASH)) cameraHelper.setZoom(1);
}
private void moveCamera (float x, float y) {
x += cameraHelper.getPosition().x;
y += cameraHelper.getPosition().y;
cameraHelper.setPosition(x, y);
}
There are two new code blocks to control the moving and zooming features of the
camera in handleDebugInput() . They look and also work similar to the block above
them that deals with the controls for the selected sprite. The keys are checked for
their state and if pressed, the respective action is taken.
The camera controls to move are as follows:
• The arrow keys left, right, up, and down control the movement of the camera
• The magnitude of motion is set to 500 percent when the Shift key is pressed
• Pressing the Backspace key resets the camera position to the origin (0, 0) of the
game world
The camera controls to zoom are as follows:
• The comma ( , ) and period ( . ) keys control the zoom level of the camera
• The magnitude of motion is set to 500 percent when the Shift key is pressed
• The forward slash ( / ) key resets the zoom level to 100 percent (the original
position)
The moveCamera() method is used to execute relative movements of the camera
similar to what moveSelectedSprite() is doing by calling sprite's translate()
method.
 
Search WWH ::




Custom Search