The Settings Screen

The settings screen offers the player to change the input method as well as enable or disable audio. We indicate this by three different icons (see Figure 12-4). Touching either the hand or the tilted phone will enable the respective input method. The icon for the currently active input method will have a gold color. For the audio icon we do the same as in the previous games.

The choices of the user are reflected by setting the respective boolean values in the Settings class. We also make sure that these settings are instantly saved to the SD card each time on of them changes via a call to Settings.save(). Listing 12-5 shows you the code.

Listing 12-5. SettingsScreen.java, the Settings 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 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 SettingsScreen extends GLScreen { Camera2D guiCam; SpriteBatcher batcher; Vector2 touchPoint; Rectangle touchBounds; Rectangle accelBounds; Rectangle soundBounds; Rectangle backBounds;

As usual we have a camera and SpriteBatcher to render our UI elements and the background. For checking whether a touch event hit a button, we also store a vector and rectangles for the three buttons on screen.

public SettingsScreen(Game game) { super(game);

guiCam = new Camera2D(glGraphics, 480, 320); batcher = new SpriteBatcher(glGraphics, 10); touchPoint = new Vector2();

touchBounds = new Rectangle(120 - 32, 160 - 32, 64, 64); accelBounds = new Rectangle(240 - 32, 160 - 32, 64, 64); soundBounds = new Rectangle(360 - 32, 160 - 32, 64, 64); backBounds = new Rectangle(32, 32, 64, 64);

In the constructor we again just set up all the members of the screen. No rocket surgery involved here.

^Override public void update(float deltaTime) {

List<TouchEvent> events = game.getInput().getTouchEvents();

TouchEvent event = events.get(i); if (event.type != TouchEvent.TOUCH_UP) continue;

guiCam.touchToWorld(touchPoint.set(event.x, event.y)); if (OverlapTester.pointInRectangle(touchBounds, touchPoint)) { Assets.playSound(Assets.clickSound); Settings.touchEnabled = true; Settings.save(game.getFileIO());

if (OverlapTester.pointInRectangle(accelBounds, touchPoint)) { Assets.playSound(Assets.clickSound); Settings.touchEnabled = false; Settings.save(game.getFileIO());

if (OverlapTester.pointInRectangle(soundBounds, touchPoint)) { Assets.playSound(Assets.clickSound); Settings.soundEnabled = !Settings.soundEnabled; if (Settings.soundEnabled) {

Assets.music.pause();

Settings.save(game.getFileIO());

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

The update() method fetches the touch events and checks whether a "touch-up" event has been registered. If so, it transforms the touch coordinates to the camera's coordinate system. With these coordinates it tests the various rectangles to decide what action to take.

^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.beginBatch(Assets.background);

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

gl.glEnable(GL10.GL_BLEND);

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

batcher.drawSprite(240, 280, 224, 32, Assets.settingsRegion); batcher.drawSprite(120, 160, 64, 64,

Settings.touchEnabIed ? Assets.touchEnabIedRegion : Assets.touchRegion); batcher.drawSprite(240, 160, 64, 64,

Settings.touchEnabIed ? Assets.acceIRegion : Assets.acceIEnabIedRegion); batcher.drawSprite(360, 160, 64, 64,

Settings.soundEnabIed ? Assets.soundEnabIedRegion : Assets.soundRegion); batcher.drawSprite(32, 32, 64, 64, Assets.IeftRegion); batcher.endBatch();

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

The render() method also does the same thing as the MainMenuScreen.render() method. We render the background and buttons with texturing and blending where needed. Based on the current settings we decide which TextureRegion to use to render the three settings buttons.

^Override public void resume() { }

^Override public void dispose() { }

The rest of the class is again composed of a few boilerplate methods with no functionality whatsoever.

Before we can create the GameScreen we have to first implement the logic and rendering of our world. Model-View-Controller to the rescue.

0 0

Post a comment