Game Development Reference
In-Depth Information
}
-(void)applicationDidBecomeActive{
[self setIsPaused:NO];
[self initializeAudio];
}
In Listing 11-4, the task
applicationWillResignActive
simply pauses the game by calling
setIsPaused
on
self
and
pause
on the
AVAudioPlayer backgroundPlayer
. When the application
becomes active again, we resume the game and then call
initializeAudio
from Listing 11-2. By
calling
initializeAudio
again, we have the chance to recheck if other audio is playing or not,
ensuring our game respects the user's wishes regarding sound.
By following the simple recipe above, we can ensure that any game will behave in a correct way
11-2
shows the example in action.
Figure 11-2.
Sample 11 in Action
In Figure
11-2
, we see four elements on our familiar space background. We see two power-ups on
the screen. These are being generated off the right side of the screen and move to the left. We also
see a spaceship on the left that will be hit by power-ups that happen to be created midway up the
screen. When a power-up collides with the spaceship, a sound is produced indicating the user got
the power-up. The power-ups also start blinking when they get about halfway across the screen.