Game Development Reference
In-Depth Information
Next, we create the
Texture
for the explosion animation, along with the
TextureRegions
for
each frame and the
Animation
instance. We simply loop from the top left to the bottom right in
64-pixel increments, and create one
TextureRegion
per frame. We then feed all the regions to an
Animation
instance, whose frame duration is 0.1 second.
shipTexture
=
new
Texture(game, "ship.png",
true
);
shipModel
= ObjLoader.
load
(game, "ship.obj");
invaderTexture
=
new
Texture(game, "invader.png",
true
);
invaderModel
= ObjLoader.
load
(game, "invader.obj");
shieldModel
= ObjLoader.
load
(game, "shield.obj");
shotModel
= ObjLoader.
load
(game, "shot.obj");
Next, we load the models and textures for the ship, the invaders, the shield blocks, and the
shots. This is pretty simple with our mighty
ObjLoader
, isn't it? Note that we use mipmapping for
the
Textures
.
music
= game.getAudio().newMusic("music.mp3");
music
.setLooping(
true
);
music
.setVolume(0.5f);
if
(Settings.
soundEnabled
)
music
.play();
clickSound
= game.getAudio().newSound("click.ogg");
explosionSound
= game.getAudio().newSound("explosion.ogg");
shotSound
= game.getAudio().newSound("shot.ogg");
}
Here, we load the music and sound effects for our game. The reference to the
Settings
class is
essentially the same as in Super Jumper and Mr. Nom. This method will be called once when our
game is started in the
AndroidInvaders
class that we'll implement in a minute. Once all assets
are loaded, we can forget about most of them, except for the
Textures
, which we need to reload
if the game is paused and then resumed.
public static void
reload() {
background
.reload();
items
.reload();
explosionTexture
.reload();
shipTexture
.reload();
invaderTexture
.reload();
if
(Settings.
soundEnabled
)
music
.play();
}
That's where the
reload()
method comes in. We call this method in the
AndroidInvaders.
onResume()
method so that our textures will be reloaded and the music will be unpaused.
public static void
playSound(Sound sound) {
if
(Settings.
soundEnabled
)
sound.play(1);
}
}