Game Development Reference
In-Depth Information
Next is the update() method. We loop through the TouchEvent s returned by our Input instance
and check for touch events. If we have such an event, we first translate the touch coordinates
to world coordinates. Since the camera is set up in such a way that we work in our target
resolution, this transformation boils down simply to flipping the y coordinate on a 320×480-pixel
screen. On larger or smaller screens, we just transform the touch coordinates to the target
resolution. Once we have our world touch point, we can check it against the rectangles of the UI
elements. If PLAY, HIGHSCORES, or HELP was touched, we transition to the respective screen.
If the sound button was pressed, we change the setting and either resume or pause the music.
Also note that we play the click sound if a UI element is pressed via the Assets.playSound()
method.
@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(160, 240, 320, 480, 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(160, 480 - 10 - 71, 274, 142, Assets. logo );
batcher.drawSprite(160, 200, 300, 110, Assets. mainMenu );
batcher.drawSprite(32, 32, 64, 64, Settings. soundEnabled ?Assets. soundOn :Assets. soundOff );
batcher.endBatch();
gl.glDisable(GL10. GL _ BLEND );
}
The present() method shouldn't really need any explanation at this point, we've done all this
before. We clear the screen, set up the projection matrices via the camera, and render the
background and UI elements. Since the UI elements have transparent backgrounds, we enable
blending temporarily to render them. The background does not need blending, so we don't use
it, to conserve some GPU cycles. Again, note that the UI elements are rendered in a coordinate
system with the origin in the lower left of the screen and the y axis pointing upward.
@Override
public void pause() {
Settings. save (game.getFileIO());
}
Search WWH ::




Custom Search