Game Development Reference
In-Depth Information
import
com.badlogic.androidgames.framework.math.Vector2;
import
com.badlogic.androidgames.jumper.World.WorldListener;
public class
GameScreen
extends
GLScreen {
static final int
GAME
_
READY
= 0;
static final int
GAME
_
RUNNING
= 1;
static final int
GAME
_
PAUSED
= 2;
static final int
GAME
_
LEVEL
_
END
= 3;
static final int
GAME
_
OVER
= 4;
int
state;
Camera2D guiCam;
Vector2 touchPoint;
SpriteBatcher batcher;
World world;
WorldListener worldListener;
WorldRenderer renderer;
Rectangle pauseBounds;
Rectangle resumeBounds;
Rectangle quitBounds;
int
lastScore;
String scoreString;
The class starts off with constants defining the five states of the screen. Next, we have the
members. We have a camera for rendering the UI elements, as well as a vector so that we can
transform touch coordinates to world coordinates (as in the other screens, to a view frustum
of 320×480 units, our target resolution). Next, we have a
SpriteBatcher
, a
World
instance, and
a
WorldListener
. The
WorldRenderer
class is something we'll look into in a minute. It basically
just takes a
World
and renders it. Note that it also takes a reference to the
SpriteBatcher
as
a parameter of its constructor. This means that we'll use the same
SpriteBatcher
to render
the UI elements of the screen and to render the game world. The rest of the members are
Rectangles
for different UI elements (such as the RESUME and QUIT menu entries on the
paused subscreen) and two members for keeping track of the current score. We want to avoid
creating a new string every frame when rendering the score so that we make the garbage
collector happy.
public
GameScreen(Game game) {
super
(game);
state =
GAME
_
READY
;
guiCam =
new
Camera2D(glGraphics, 320, 480);
touchPoint =
new
Vector2();
batcher =
new
SpriteBatcher(glGraphics, 1000);
worldListener =
new
WorldListener() {
public void
jump() {
Assets.
playSound
(Assets.
jumpSound
);
}
public void
highJump() {
Assets.
playSound
(Assets.
highJumpSound
);
}