Assets A Convenient Asset Store

The loading screen will load all the assets of our game. But where do we store them? To store them, we'll do something that is not seen very often in Java land: we'll create a class that has a ton of static public members that hold all the Pixmaps and Sounds that we've loaded from the assets. Listing 6-2 shows you that class.

Listing 6-2. Assets.java, Holding All Our Pixmaps and Sounds for Easy Access package com.badlogic.androidgames.mrnom;

import com.badlogic.androidgames.framework.Pixmap; import com.badlogic.androidgames.framework.Sound;

public class Assets {

public static Pixmap background;

public static Pixmap logo;

public static Pixmap mainMenu; public static Pixmap buttons; public static Pixmap help1; public static Pixmap help2; public static Pixmap help3; public static Pixmap numbers; public static Pixmap ready; public static Pixmap pause; public static Pixmap gameOver; public static Pixmap headUp; public static Pixmap headLeft; public static Pixmap headDown; public static Pixmap headRight; public static Pixmap tail; public static Pixmap stain1; public static Pixmap stain2; public static Pixmap stain3;

public static Sound click; public static Sound eat; public static Sound bitten;

We have a static member for every image and sound we load from the assets. If we want to use one of these assets, we can do something like this:

game.getGraphics().drawPixmap(Assets.background, 0, 0)

or something like this:

Assets.click.play(1);

Now, that's convenient. However, note that nothing is keeping us from overwriting those static members, as they are not final. But as long as we don't overwrite them, we are safe. These public, non-final members make this "design pattern" an antipattern, actually. For our game it's OK to be a little lazy, though. A cleaner solution would hide the assets behind setters and getters in a so-called singleton class. We'll stick to our poor-man's asset manager, though.

Was this article helpful?

0 0

Post a comment