Game Development Reference
In-Depth Information
The code will check whether keycode contains the code for either R or the Space bar.
If it is R , the initialization method init() of WorldController is called. This results
in an internal restart of the game as if the whole game was restarted. If the Space
bar was pressed, the index stored in selectedSprite is incremented by one. The
modulo operator ( % ) that is followed by the size of the array is used to wrap around
the incremented value if it exceeds the maximum value allowed.
This handler method is called only when there is an event. This is the
huge difference as compared to the previous way we were handling
user input. Both ways are correct, as it only depends on your situation
and how you need the input in question to be handled.
You can now start the game and try it out on your desktop. You should be able to
reset the game world with the R key. The test sprites should be shuffling around
on every executed reset action. Selecting another sprite using the Space bar should
make the previously selected one stop rotating and in turn, let the newly selected
sprite start rotating. You can also use the keys A , D , W , and S that will only move the
currently selected sprite one at a time.
Adding the CameraHelper class
We now want to implement a helper class called CameraHelper that will assist us
to manage and manipulate certain parameters of the camera we use to render the
game world.
Here is the implementation of CameraHelper :
package com.packtpub.libgdx.canyonbunny.util;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
public class CameraHelper {
private static final String TAG = CameraHelper.class.getName();
private final float MAX_ZOOM_IN = 0.25f;
private final float MAX_ZOOM_OUT = 10.0f;
private Vector2 position;
private float zoom;
 
Search WWH ::




Custom Search