Game Development Reference
In-Depth Information
new
TextureRegion(
items
, 0, 160, 32, 32),
new
TextureRegion(
items
, 32, 160, 32, 32));
platform
=
new
TextureRegion(
items
, 64, 160, 64, 16);
brakingPlatform
=
new
Animation(0.2f,
new
TextureRegion(
items
, 64, 160, 64, 16),
new
TextureRegion(
items
, 64, 176, 64, 16),
new
TextureRegion(
items
, 64, 192, 64, 16),
new
TextureRegion(
items
, 64, 208, 64, 16));
font
=
new
Font(
items
, 224, 0, 16, 16, 20);
music
= game.getAudio().newMusic("music.mp3");
music
.setLooping(
true
);
music
.setVolume(0.5f);
if
(Settings.
soundEnabled
)
music
.play();
jumpSound
= game.getAudio().newSound("jump.ogg");
highJumpSound
= game.getAudio().newSound("highjump.ogg");
hitSound
= game.getAudio().newSound("hit.ogg");
coinSound
= game.getAudio().newSound("coin.ogg");
clickSound
= game.getAudio().newSound("click.ogg");
}
The
load()
method, which will be called once at the start of our game, is responsible for
populating all the static members of the class. It loads the background image and creates
a corresponding
TextureRegion
for it. Next, it loads the texture atlas and creates all the
necessary texture regions and animations. Compare the code to what's shown in Figure
9-15
and the other figures in the previous section. The only noteworthy thing about the code for
loading graphical assets is the creation of the coin
Animation
instance. As discussed, we reuse
the second frame at the end of the animation frame sequence. All the animations use a frame
time of 0.2 seconds.
We also create an instance of the
Font
class, which we have not yet discussed. It will implement
the logic to render text with the bitmap font embedded in the atlas. The constructor takes the
Texture
, which contains the bitmap font glyphs, the pixel coordinates of the top-left corner of
the area that contains the glyphs, the number of glyphs per row, and the size of each glyph in
pixels.
We also load all the
Music
and
Sound
instances in that method. As you can see, we work with our
old friend the
Settings
class again. We can reuse it from the Mr. Nom project pretty much as is,
with one slight modification, as you'll see in a minute. Note that we set the
Music
instance to be
looping and its volume to 0.5, so it is a little quieter than the sound effects. The music will start
playing only if the user hasn't previously disabled the sound, which is stored in the
Settings
class, as in Mr. Nom.
public static void
reload() {
background
.reload();
items
.reload();
if
(Settings.
soundEnabled
)
music
.play();
}