Game Development Reference
In-Depth Information
And after creating this, check for errors, if any, print an error message, and return
from there.
NSError *error;
self.coinCollectedAudioPlayer = [[AVAudioPlayer
alloc] initWithContentsOfURL:file error:&error];
if (error)
{
NSLog(@"Error in audio play %@",[error userInfo]);
return;
}
13. After that, call prepareToPlay on that object, set some volume such as 1.0
and play it once you set numberOfLoops to 1 .
[self.coinCollectedAudioPlayer prepareToPlay];
self.coinCollectedAudioPlayer.numberOfLoops = 1;
self.coinCollectedAudioPlayer.volume = 1.0;
14. Finally, after all of this, play the sound effect that has to be played when the coin
is collected by the spaceship.
[self.coinCollectedAudioPlayer play];
15. After all of this, gather the code in a function called playCoinCollec-
tedSoundEffect and it will look like this:
16. To make it play when the coin collides with the spaceship, call the
playCoinCollectedSoundEffect function when spaceShipCol-
lidedWithCoin is called by the detection of a collision.
17. After all the integration of background music and sound effects, our starter kit for
this chapter is ready.
Search WWH ::




Custom Search