The Main Menu Screen

We've already written many trivial screens in the previous games. Droid Invaders also has some of these. The principle is always the same: offer some UI elements to click and trigger transitions or configuration changes and display some information. The main menu screen presents only the logo and the Play and Settings options shown in Figure 12-4. Touching one of these buttons triggers a transition to the GameScreen or the SettingsScreen. Listing 12-4 shows the code.

Listing 12-4., the Main Menu Screen package com.badlogic.androidgames.droidinvaders;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.badlogic.androidgames.framework.Game; import com.badlogic.androidgames.framework.Input.TouchEvent; import; import; 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 virtual target resolution of 480x320 pixels. We use a SpriteBatcher to render the UI elements and background image. The Vector2 and Rectangle instances will help us to decide whether a touch hit a button or not.

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 setup the camera and SpriteBatcher as we always do. We also instantiate the Vector2 and the Rectangles, using the position and width and height of the two elements on screen in our 480x320 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;

guiCam.touchToWorld(touchPoint.set(event.x, event.y)); if(OverlapTester.pointiMectangIe(playBounds, touchPoint)) { Assets.pIaySound(Assets.cIickSound); game.setScreen(new GameScreen(game));

if(OverlapTester.pointInRectangle(settingsBounds, touchPoint)) { Assets.playSound(Assets.clickSound); game.setScreen(new SettingsScreen(game));

In the update() method we fetch the touch events and check for "touch-up" events. If there is such an event we transform its real coordinates to the coordinate system the camera sets up. All that's left is checking the touch point against the two rectangles bounding the menu entries. If one of them is hit we play the click sound and transition to the respective screen.

^Override public void present(float deltaTime) { GL10 gl = glGraphics.getGL(); gl.glClear(GL10. GL_COLOR_BUFFER_BIT); guiCam.setViewportAndMatrices();

gl.glEnable(GL10. GL_TEXTURE_2D);


batcher.drawSprite(240, 160, 480, 320, Assets.backgroundRegion); batcher.endBatch();


gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); batcher.beginBatch(Assets.items);

batcher.drawSprite(240, 240, 384, 128, Assets.logoRegion);

batcher.drawSprite(240, 100, 224, 64, Assets.menuRegion); batcher.endBatch();

gl.glDisable(GL10.GL_BLEND); gl.glDisable(GL10. GL_TEXTURE_2D);

The present() method does the same thing we did in most screens of Super Jumper. We clear the screen and set up the projection matrix via our camera. We enable texturing and then immediately render the background via the SpriteBatcher and TextureRegion defined in the Assets class. The menu items have translucent areas, so we enable blending before we render them.

^Override public void resume() { }

^Override public void dispose() { }

The rest of the class consists of boilerplate methods that don't do anything. Texture reloading is done in the DroidInvaders activity, so there isn't anything left to take care of in the MainMenuScreen.

0 0

Post a comment