Game Development Reference
In-Depth Information
We store the
GLGraphics
and
GLGame
instances in the
GLScreen
class. Of course, this will crash if
the
Game
instance passed as a parameter to the constructor is not a
GLGame
. But we'll make sure
it is. All the screens of Super Jumper will derive from this class.
The Main Menu Screen
The main menu screen is the screen that is returned by
SuperJumper.getStartScreen()
, so it's the
first screen the player will see. It renders the background and UI elements and simply waits there
for the player to touch any of the UI elements. Based on the element that was touched, the game
either changes the configuration (sound enabled/disabled) or transitions to a new screen. Listing
9-6 shows the code.
Listing 9-6. MainMenuScreen.java, the Main Menu Screen
package
com.badlogic.androidgames.jumper;
import
java.util.List;
import
javax.microedition.khronos.opengles.GL10;
import
com.badlogic.androidgames.framework.Game;
import
com.badlogic.androidgames.framework.Input.TouchEvent;
import
com.badlogic.androidgames.framework.gl.Camera2D;
import
com.badlogic.androidgames.framework.gl.SpriteBatcher;
import
com.badlogic.androidgames.framework.impl.GLScreen;
import
com.badlogic.androidgames.framework.math.OverlapTester;
import
com.badlogic.androidgames.framework.math.Rectangle;
import
com.badlogic.androidgames.framework.math.Vector2;
public class
MainMenuScreen
extends
GLScreen {
Camera2D guiCam;
SpriteBatcher batcher;
Rectangle soundBounds;
Rectangle playBounds;
Rectangle highscoresBounds;
Rectangle helpBounds;
Vector2 touchPoint;
The class derives from
GLScreen
, so we can access the
GLGraphics
instance more easily.
There are a couple of members in this class. The first one is a
Camera2D
instance called
guiCam
.
We also need a
SpriteBatcher
to render our background and UI elements. We'll use rectangles
to determine if the user touched a UI element. Since we use a
Camera2D
, we also need a
Vector2
instance to transform the touch coordinates to world coordinates.
public
MainMenuScreen(Game game) {
super(game);
guiCam =
new
Camera2D(glGraphics, 320, 480);
batcher =
new
SpriteBatcher(glGraphics, 100);
soundBounds =
new
Rectangle(0, 0, 64, 64);