Game Development Reference
In-Depth Information
In Listing 11-11, the task playAudioInBackground: is responsible for actually configuring the
AVAudioPlayer and finally calling play on it. As mentioned, this task is called in a background thread.
This is done because it greatly improves the performance of the application. If this were called on the
same thread the game is running in, you would see considerable jerkiness in the game's animations.
After pulling the player , volume , and pan objects out of the NSArray params , we prepare the player
to play the audio. We set the currentTime property to 0, the start of the track; we also set the volume
and pan values to be used. The volume value is from silent (0.0) to full volume (1.0). By full volume,
we mean the maximum volume given the user's volume settings. The pan value is like the balance
knob on a stereo. The value −1.0 indicates all the way to the left, the value 0.0 indicates evenly left
and right, and 1.0 indicates all the way to the right. (We will use this feature when we look at the
Powerup's blinking behavior.)
play on player , which caused the sound to be emitted by the iOS device. Notice
AVAudioPlayer per type of sound—meaning that it is possible for a
AVAudioPlayer to be part way through playing when it is called to start over. This is a bit of a
AVAudioPlayer
AVAudioPlayers for each type of sound,
GameController would be responsible for figuring out which (if any) AVAudioPlayer is
governing what to do when an AVAudioPlayer is unavailable, will vary greatly in different games and
for different sounds. I leave it up to the reader to extend GameController if this is a required feature
for a game. Let me know what you come up with! You can email me at lucasjordan@gmail.com .
Audio Driven by an Actor
In our example, we have considered the simplest example of playing. We looked at playing a sound
when a Powerup collides with the spaceship; this sound effect was called by the ViewController .
One of the overarching design decisions being presented in this topic is that actors should
encapsulate as much logic as they can, being free agents in the scene to do as they wish, and
this includes playing sounds. There is nothing interesting from a technical perspective about the
following example, but it will illustrate how sound effects can be driven by a particular actor's state.
Let's explore the sound that occurs when a Powerup blinks; this is shown in Listing 11-12.
Listing 11-12. step: (Powerup.m)
-(void)step:(GameController*)gameController{
if (self.center.x < −self.radius){
[gameController removeActor:self];
return;
}
 
Search WWH ::




Custom Search