Game Development Reference
In-Depth Information
Your Game Is Not the Only Application
In addition to honoring the background music, the game should consider the different ways that
the game can be interrupted and still behave correctly. Consider the ways that a game may be
interrupted:
1.
The user exits the game with the Home button.
2.
The user double-clicks the Home button to reveal the background
applications.
3.
The user does a four-finger swipe (iPad only) to switch to another application.
4.
The user receives a popup warning, such as low power.
5.
The user receives a phone call.
All of these events can be summarized as the game's resigning its active status. Conversely, when
the user returns to the game, it is said to become active. These ideas, of course, are captured
in the tasks applicationWillResignActive : and applicationDidBecomeActive : of the class
UIApplicationDelegate . In the following section, we will review how we use these tasks to ensure
correct application behavior.
Implementing Sound in Your Game
We have discussed some of the finer points of audio in an iOS game. We have looked at the
expected behavior concerning the hardware controls, headphones, and different ways a game
interacts with other applications. In this section, we will explore how these concerns are addressed
in code. We will also look at how the class GameController can be extended to provide a simple
audio framework for your game.
Setting Up Audio
The first thing that any game should do regarding audio is to define how audio should be played
by the application. iOS offers many different ways that applications can play sound to support
different categories of applications. For example, the built-in Music application plays audio while
other applications are active. Further, a sound-editing application or Garage Band has different
requirements. A game application has yet a different set of requirements. Listing 11-2 shows how a
game should initialize its audio session.
Listing 11-2. initializeAudio: (GameController.m)
-(void)initializeAudio{
AVAudioSession* session = [AVAudioSession sharedInstance];
[session setCategory: AVAudioSessionCategoryAmbient error: nil];
[session setDelegate:self];
 
Search WWH ::




Custom Search