As usual, 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, just return the MainMenuScreen
, which you will implement shortly. One thing to remember is the definition of the activity in the manifest file. Make sure that you have the orientation set to landscape
! Listing 12–3 shows you the code.
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 DroidInvaders 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();
}
}
This is exactly the same as in Super Jumper. On a call to getStartScreen()
, return a new instance of the MainMenuScreen
that you'll write next. In onSurfaceCreated()
, make sure your assets are reloaded, and in onPause()
, pause the music if it is playing.
As you can see, there are a lot of things that can be repeated once you have a good idea how to approach the implementation of a simple game. Think about how you could reduce the boilerplate code even more by moving things to the framework!