The Main Activity

As usual we have a main activity that derives from the GLGame class. It is responsible for loading the assets through a call to Assets.load() on startup as well as pausing and resuming the music when the activity is paused or resumed. As the start screen we just return the MainMenuScreen, which we will implement shortly. One thing to remember is the definition of the activity in the manifest file. Make sure you have the orientation set to landscape! Listing 12-3 shows you the code.

Listing 12-3. Droidlnvaders.java, the Main Activity package com.badlogic.androidgames.droidinvaders;

import javax.microedition.khronos.egl.EGLConfig;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.Screen; import com.badlogic.androidgames.framework.impl.GLGame;

public class Droidlnvaders extends GLGame {

boolean firstTimeCreate = true;

^Override public Screen getStartScreen() { return new MainMenuScreen(this);

^Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { super.onSurfaceCreated(gl, config); if (firstTimeCreate) {

Settings.load(getFileIO()); Assets.load(this); firstTimeCreate = false; } else {

Assets.reload();

^Override public void onPause() { super.onPause(); if (Settings.soundEnabled) Assets.music.pause();

That's exactly the same as in Super Jumper. On a call to getStartScreen() we return a new instance of the MainMenuScreen we'll write next. In onSurfaceCreated() we make sure our assets are reloaded and in onPause() we pause the music if it is playing.

As you can see, there are a lot of things that get repeated once we have a good idea how to approach implementing a simple game. Think about how you could reduce the boilerplate code even more by moving things to the framework!

0 0

Post a comment