Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

GenoFloresAZ

macrumors newbie
Original poster
Mar 15, 2014
14
0
Phoenix, Arizona
I'm making a game for iOS, everything is finished except sounds. The app already plays a sound when buttons are pressed, but I need another sound file to be played when the bee (main UIImage character) comes in contact with the coin (object of the game is to catch these coins). And also I need another sound file to be played when the bee comes in contact with one of the two obstacles. How do I go on about doing this?
 
In your method where you determine what to do when contact is made you can do something like this:

Code:
NSError *error = nil;
    NSURL *pointURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.wav", [[NSBundle mainBundle]resourcePath]]];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:pointURL error:&error];
    [audioPlayer prepareToPlay];
    [audioPlayer play];
 
In your method where you determine what to do when contact is made you can do something like this:

Code:
NSError *error = nil;
    NSURL *pointURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.wav", [[NSBundle mainBundle]resourcePath]]];
    audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:pointURL error:&error];
    [audioPlayer prepareToPlay];
    [audioPlayer play];

That code will work but I would be inclined to use the NSBundle method URLForResource:withExtension instead:


Code:
NSURL *pointURL = [[NSBundle mainBundle]  URLForResource: @"sound" 
  withExtension: @"wav"];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.