Game Development Reference
In-Depth Information
import
com.badlogic.androidgames.framework.Game;
import
com.badlogic.androidgames.framework.Input.TouchEvent;
import
com.badlogic.androidgames.framework.gl.Camera2D;
import
com.badlogic.androidgames.framework.gl.SpriteBatcher;
import
com.badlogic.androidgames.framework.impl.GLScreen;
import
com.badlogic.androidgames.framework.math.OverlapTester;
import
com.badlogic.androidgames.framework.math.Rectangle;
import
com.badlogic.androidgames.framework.math.Vector2;
public class
HighscoreScreen
extends
GLScreen {
Camera2D guiCam;
SpriteBatcher batcher;
Rectangle backBounds;
Vector2 touchPoint;
String[] highScores;
float
xOffset = 0;
As always, we have a couple of members for the camera, the
SpriteBatcher
, bounds for
the arrow button, and so on. In the
highScores
array, we store the formatted strings for each
high score we present to the player. The
xOffset
member is a value we compute to offset the
rendering of each line so that the lines are centered horizontally.
public
HighscoreScreen(Game game) {
super
(game);
guiCam =
new
Camera2D(glGraphics, 320, 480);
backBounds =
new
Rectangle(0, 0, 64, 64);
touchPoint =
new
Vector2();
batcher =
new
SpriteBatcher(glGraphics, 100);
highScores =
new
String[5];
for
(
int
i = 0; i < 5; i++) {
highScores[i] = (i + 1) + ". " + Settings.
highscores
[i];
xOffset = Math.
max
(highScores[i].length() * Assets.
font
.glyphWidth, xOffset);
}
xOffset = 160 - xOffset / 2;
}
In the constructor, we set up all members as usual and compute that
xOffset
value. We do so by
evaluating the size of the longest string out of the five strings we create for the five high scores.
Since our bitmap font is fixed-width, we can easily calculate the number of pixels needed for
a single line of text by multiplying the number of characters with the glyph width. This will, of
course, not account for nonprintable characters or characters outside of the ASCII character set.
Since we know that we won't be using those, we can get away with this simple calculation. The
last line in the constructor then subtracts half of the longest line width from 160 (the horizontal
center of our target screen of 320×480 pixels) and adjusts it further by subtracting half of the
glyph width. This is needed since the
Font.drawText()
method uses the glyph centers instead of
one of the corner points.