Game Development Reference
In-Depth Information
The helper class stores the current position and zoom value for the camera.
Furthermore, it can follow one game object at a time when set as a target by calling
setTarget()
. The target can also be set to
null
to make the camera stop following
at all. To find out what the last set target is, you can call
getTarget()
. Usually, you
will want to do this for
null
checks or to find out whether the set target is a certain
sprite. These checks are wrapped into the
hasTarget()
method and can be used
either with or without a sprite argument to find out whether a certain target has
been set if any. The
update()
method should be called on every update cycle to let
it update the camera position whenever needed. The
applyTo()
method should
always be called at the beginning of the rendering of a new frame as it takes care of
updating the camera's attributes.
Adding the camera debug controls using
CameraHelper
The last step in this chapter will be to add the camera debug controls using the
CameraHelper
class. This will greatly improve your debugging abilities just because
you can freely move around the game world, zoom in and out to/from game objects,
and follow any game object.
Before we can use
CameraHelper
, we have to import it in
WorldController
as
follows:
import com.packtpub.libgdx.canyonbunny.util.CameraHelper;
After this, add the following code to
WorldController
:
public CameraHelper cameraHelper;
private void init () {
Gdx.input.setInputProcessor(this);
cameraHelper = new CameraHelper();
initTestObjects();
}
public void update (float deltaTime) {
handleDebugInput(deltaTime);
updateTestObjects(deltaTime);
cameraHelper.update(deltaTime);
}