The Assets Class

In Mr. Nom we already had an Assets class that consisted only of a metric ton of Pixmap and Sound references held in static member variables. We'll do the same in Super Jumper. This time we'll add a little loading logic, though. Listing 9-1 shows the code.

Listing 9-1., Which Holds All Our Assets Except for the Help Screen Textures package com.badlogic.androidgames.jumper;

import com.badlogic.androidgames.framework.Music; import com.badlogic.androidgames.framework.Sound; import; import; import; import; import com.badlogic.androidgames.framework.impl.GLGame;

public class Assets {

public static Texture background;

public static TextureRegion backgroundRegion;

public static Texture items; public static TextureRegion mainMenu; public static TextureRegion pauseMenu; public static TextureRegion ready; public static TextureRegion gameOver; public static TextureRegion highScoresRegion; public static TextureRegion logo; public static TextureRegion soundOn; public static TextureRegion soundOff; public static TextureRegion arrow; public static TextureRegion pause; public static TextureRegion spring; public static TextureRegion castle; public static Animation coinAnim; public static Animation bobJump; public static Animation bobFall; public static TextureRegion bobHit; public static Animation squirrelFly; public static TextureRegion platform; public static Animation brakingPlatform; public static Font font;

public static Music music; public static Sound jumpSound; public static Sound highJumpSound; public static Sound hitSound; public static Sound coinSound; public static Sound clickSound;

The class holds references to all the Texture, TextureRegion, Animation, Music, and Sound instances we need throughout our game. The only thing we don't load here are the images for the help screens.

public static void load(GLGame game) {

background = new Texture(game, "backgrounding"); backgroundRegion = new TextureRegion(background, 0, 0, 320, 480);

items = new Texture(game, "items.png");

mainMenu = new TextureRegion(items, 0, 224, 300, 110);

pauseMenu = new TextureRegion(items, 224, 128, 192, 96);

ready = new TextureRegion(items, 320, 224, 192, 32);

gameOver = new TextureRegion(items, 352, 256, 160, 96);

highScoresRegion = new TextureRegion(Assets.items, 0, 257, 300, 110 / 3);

logo = new TextureRegion(items, 0, 352, 274, 142);

soundOff = new TextureRegion(items, 0, 0, 64, 64);

soundOn = new TextureRegion(items, 64, 0, 64, 64);

spring = new TextureRegion(items, 128, 0, 32, 32); castle = new TextureRegion(items, 128, 64, 64, 64); coinAnim = new Animation(0.2f, new TextureRegion(items, 128, 32, 32, 32), new TextureRegion(items, 160, 32, 32, 32), new TextureRegion(items, 192, 32, 32, 32), new TextureRegion(items, 160, 32, 32, 32)); bobJump = new Animation(0.2f, new TextureRegion(items, 0, 128, 32, 32), new TextureRegion(items, 32, 128, 32, 32)); bobFall = new Animation(0.2f, new TextureRegion(items, 64, 128, 32, 32), new TextureRegion(items, 96, 128, 32, 32)); bobHit = new TextureRegion(items, 128, 128, 32, 32); squirrelFly = new Animation(0.2f, 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));

music = game.getAudio().newMusic("music.mp3"); music.setLooping(true); music.setVolume(0.5f); if(Settings.soundEnabled); 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 TextureRegions and Animations. Compare the code to Figure 9-15 and the other figures in the last 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 discussed yet. 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 only start playing 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);

Next we have a mysterious method called reload(). Remember that the OpenGL ES context will get lost when our application is paused. We have to reload the textures when the application is resumed, and that's exactly what this method does. We also resume the music playback in case sound is enabled.

public static void playSound(Sound sound) { if(Settings.soundEnabled);

The final method of this class is a helper method we'll use in the rest of the code to play back audio. Instead of having to check whether sound is enabled everywhere, we encapsulate that check in this method.

Let's have a look at the modified Settings class.

0 0

Post a comment