Loading Screen Fetching the Assets from Disk

With those classes at hand, we can now easily implement the loading screen. Listing 64 shows you the code.

Listing 6-4. LoadingScreen.java, Which Loads All Assets and the Settings package com.badlogic.androidgames.mrnom;

import com.badlogic.androidgames.framework.Game;

import com.badlogic.androidgames.framework.Graphics;

import com.badlogic.androidgames.framework.Screen;

import com.badlogic.androidgames.framework.Graphics.PixmapFormat;

public class LoadingScreen extends Screen { public LoadingScreen(Game game) { super(game);

We let the LoadingScreen class derive from the Screen class we defined in Chapter 3. This requires that we implement a constructor that takes a Game instance, which we hand to the superclass constructor. Note that this constructor will be called in the MrNomGame.getStartScreen() method we defined earlier.

@Override public void update(float deltaTime) { Graphics g = game.getGraphics();

Assets.background = g.newPixmap("background.png", PixmapFormat.RGB565); Assets.logo = g.newPixmap("logo.png", PixmapFormat.ARGB4444); Assets.mainMenu = g.newPixmap("mainmenu.png", PixmapFormat.ARGB4444); Assets.buttons = g.newPixmap("buttons.png", PixmapFormat.ARGB4444); Assets.help1 = g.newPixmap("help1.png", PixmapFormat.ARGB4444); Assets.help2 = g.newPixmap("help2.png", PixmapFormat.ARGB4444); Assets.help3 = g.newPixmap("help3.png", PixmapFormat.ARGB4444); Assets.numbers = g.newPixmap("numbers.png", PixmapFormat.ARGB4444); Assets.ready = g.newPixmap("ready.png", PixmapFormat.ARGB4444); Assets.pause = g.newPixmap("pausemenu.png", PixmapFormat.ARGB4444); Assets.gameOver = g.newPixmap("gameover.png", PixmapFormat. ARGB4444); Assets.headUp = g.newPixmap("headup.png", PixmapFormat.ARGB4444); Assets.headLeft = g.newPixmap("headleft.png", PixmapFormat.ARGB4444); Assets.headDown = g.newPixmap("headdown.png", PixmapFormat.ARGB4444); Assets.headRight = g.newPixmap("headright.png", PixmapFormat.ARGB4444); Assets.tail = g.newPixmap("tail.png", PixmapFormat.ARGB4444); Assets.stain1 = g.newPixmap("stain1.png", PixmapFormat.ARGB4444); Assets.stain2 = g.newPixmap("stain2.png", PixmapFormat.ARGB4444); Assets.stain3 = g.newPixmap("stain3.png", PixmapFormat.ARGB4444); Assets.click = game.getAudio().newSound("click.ogg"); Assets.eat = game.getAudio().newSound("eat.ogg"); Assets.bitten = game.getAudio().newSound("bitten.ogg"); Settings.load(game.getFileIO()); game.setScreen(new MainMenuScreen(game));

Next up is our implementation of the update() method, where we load the assets and settings. For the image assets, we simply create new Pixmaps via the Graphics.newPixmap() method. Note that we specify which color format the Pixmaps should have. The background has an RGB565 format, and all other images have an ARGB4444 format (if the BitmapFactory respects our hint). We do this conserve memory and increase our rendering speed a little later on. Our original images are stored in RGB888 and ARGB8888 format as PNGs. We also load in the three sound effects and store them in the respective members of the Assets class. Next we load the settings from the external storage via the Settings.load() method. Finally we initiate a screen transition to a Screen called MainMenuScreen, which will take over execution from that point on.

@Override public void present(float deltaTime) { }

@Override public void resume() { }

@Override public void dispose() { }

The other methods are just stubs and do not perform any actions. Since the update() method will immediatly trigger a screen transition after all assets are loaded, there's nothing more to do on this screen.

0 0

Post a comment