Game Development Reference
In-Depth Information
Listing 12-4. MainMenuScreen.java, the Main Menu Screen
package
com.badlogic.androidgames.androidinvaders;
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;
Vector2 touchPoint;
Rectangle playBounds;
Rectangle settingsBounds;
As usual, we need a camera to set up our viewport and the virtual target resolution of
480×320 pixels. We use a
SpriteBatcher
to render the UI elements and background image.
The
Vector2
and
Rectangle
instances will help us decide whether a touch hit a button.
public
MainMenuScreen(Game game) {
super
(game);
guiCam =
new
Camera2D(glGraphics, 480, 320);
batcher =
new
SpriteBatcher(glGraphics, 10);
touchPoint =
new
Vector2();
playBounds =
new
Rectangle(240-112, 100, 224, 32);
settingsBounds =
new
Rectangle(240-112, 100-32, 224, 32);
}
In the constructor, we set up the camera and the
SpriteBatcher
, as we always do. We
instantiate the
Vector2
and the
Rectangle
instances using the position, width, and height of the
two elements onscreen, in our 480×320 target resolution.
@Override
public void
update(
float
deltaTime) {
List<TouchEvent> events = game.getInput().getTouchEvents();
int
len = events.size();
for
(
int
i = 0; i < len; i++) {
TouchEvent event = events.get(i);
if
(event.type != TouchEvent.
TOUCH_UP
)
continue
;