-1

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];

}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Romano Vacca
  • 305
  • 1
  • 4
  • 11

3 Answers3

1

What you could do here is to use completion block.

For example:

Define one:

typedef void (^CompletionBlock)();


- (void)playSoundWhenMonsterHitsGround:(CompletionBlock)completionBlock
{
    [self.monster runAction:self.gameoverSound];
    completionBlock();
}

And in your -(void)monster:(SKSpriteNode *)monster didCollideWithbottomGround:(SKSpriteNode *)bottomGround:

-(void)monster:(SKSpriteNode *)monster didCollideWithbottomGround:(SKSpriteNode *)bottomGround 
{
    [self resetDuration];
    [_monster removeFromParent];
    [self playSoundWhenMonsterHitsGround:^
    {
        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];
    }];
}

Hope this helps.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • hi thanks for your help, i implemented it but i get the error after completion(); : implicit declaration of function "completion" is invalid in C99 – Romano Vacca Jul 12 '14 at 10:18
  • The error is gone, but it still goes to the other scene without the gameoversound. – Romano Vacca Jul 12 '14 at 10:32
  • i used the runAction you gave me - (void)playSoundWhenMonsterHitsGround:(CompletionBlock)completionBlock { [self.monster runAction:self.gameoverSound]; completionBlock(); } – Romano Vacca Jul 12 '14 at 11:09
  • what you gave me is my only runAction for this gameover sound. – Romano Vacca Jul 12 '14 at 11:25
  • @RomanoVacca No, look at your own code. runAction is defined by you, not me. Did you even write the code yourself? – Unheilig Jul 12 '14 at 11:34
  • I got it working, the problem was i did not have a runAction which you pointed out. I understand it better now thanks! – Romano Vacca Jul 13 '14 at 16:30
0
SystemSoundID soundToPlay;

NSString *path  = [[NSBundle mainBundle] pathForResource:@"gameover1" ofType:@"wav"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &soundToPlay);

//playback
AudioServicesPlaySystemSound(soundToPlay);
Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
ganeshIOS
  • 136
  • 2
0

SKAction *sound = [SKAction playSoundFileNamed:@"fileName" waitForCompletion:NO];

You can experiment with the waitForCompletion: until it fits you liking.