Game Development Reference
In-Depth Information
[self.backgroundAudioPlayer prepareToPlay];
self.backgroundAudioPlayer.numberOfLoops = -1;
self.backgroundAudioPlayer.volume = 1.0;
6. Finally, after all of this, play the background music that never ends.
[self.backgroundAudioPlayer play];
7. After this, gather the code in a function called startBackgroundMusic and
it will look like this:
8. To make it play the background music, call the startBackgroundMusic
function in the initWithSize method of FSMyScene .
9. Now, compile and run the project; you should be able to listen to the background
music in the game.
10. Now we will add a sound effect in the game when a coin is collected. For this, de-
clare a property named coinCollectedAudioPlayer as an object of AVAu-
dioPlayer.
@property (nonatomic, strong) AVAudioPlayer
*coinCollectedAudioPlayer;
11. As explained in the code snippet in the AVAudioPlayer section, create a path and
a NSURL file using coin-collected-sound.caf file.
NSString *samplePath = [[NSBundle mainBundle]
pathForResource:@"coin-collected-sound.caf"
ofType:nil];
NSURL *file = [NSURL fileURLWithPath:samplePath];
12. Assign an AVAudioPlayer object with the preceding sound effect file with the
error as the parameter to the self.coinCollectedAudioPlayer property.
Search WWH ::




Custom Search