I made a game with sprite kit. Now I am trying to implement some sound effects. I managed to do some sound effect but I got stuck at the part where I have to implement a sound whenever you lose. So you lose when an object touches the ground. But when that happens it transitions to another scene. So I want the gameover sound to play BEFORE it transitions to the other scene. This is what I got :
Myscene.h
#import <AVFoundation/AVFoundation.h>
@interface MyScene : SKScene<SKPhysicsContactDelegate>
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@property (strong, nonatomic) SKAction *catchSound;
@property (strong, nonatomic) SKAction *gameoverSound;
Myscene.m
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.catchSound = [SKAction playSoundFileNamed:@"166331__lokemon44__mushroom.wav" waitForCompletion:NO];
self.gameoverSound = [SKAction playSoundFileNamed:@"gameover1.wav" waitForCompletion:NO];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"select" withExtension:@"wav"];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!self.audioPlayer) {
NSLog(@"Error creating player: %@", error);
}
}
return self;
}
-(void)didBeginContact:(SKPhysicsContact *)contact{
if ((firstBody.categoryBitMask == monsterCategory) != 0 &&
(secondBody.categoryBitMask == bottomCategory) != 0)
{
[self monster:(SKSpriteNode *) firstBody.node didCollideWithbottomGround:(SKSpriteNode *) secondBody.node];
}
}
-(void)monster:(SKSpriteNode *)monster didCollideWithbottomGround:(SKSpriteNode *)bottomGround {
[self.monster runAction:self.gameoverSound];
[self resetDuration];
[_monster removeFromParent];
SKTransition *reveal5 = [SKTransition fadeWithDuration:0.5];
SKScene * InstructionSceneL = [[GameOverScene alloc] initWithSize:self.size score:player_score];
InstructionSceneL.scaleMode = SKSceneScaleModeAspectFill;
[self.view presentScene:InstructionSceneL transition:reveal5];
}