Pausa y Reanudación de un juego. Java Game 8
Incluso, en el juego más emocionante, llega un momento en que el usuario quiere pausar y reanudar más adelante. La documentación de Java para la clase Thread recomienda utilizar wait() y notify() para implementar la pausa y reanudar la funcionalidad. La idea es suspender la animación, pero el hilo del distribuidor de eventos aún responderá a la actividad del GUI (escritorio). Para implementar este enfoque, se puede introducir un booleano isPaused, que se establece en true vía el método pauseGame(): // global variable private volatile boolean isPaused = false; public void pauseGame( ){ isPaused = true; } private void testPress(int x, int y){ // is (x,y) important to the game? if (!isPaused && !gameOver) { // ... } } private void gameUpdate( ){ if (!isPaused && !gameOver) // update game state ... } public synchronized void resumeGame( ){ isPaused = false; notify( ); } public synchronized void stopGame( ){ running = false; ...