-1

我用精灵套件做了一个游戏。现在我正在尝试实现一些声音效果。我设法做了一些声音效果,但我陷入了每当你输球时我必须实现声音的部分。因此,当物体接触地面时,您会失败。但是当这种情况发生时,它会过渡到另一个场景。所以我希望在过渡到另一个场景之前播放游戏结束声音。这就是我得到的:

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

}
4

3 回答 3

1

你可以在这里做的是使用完成块。

例如:

定义一个:

typedef void (^CompletionBlock)();


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

在你的-(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];
    }];
}

希望这可以帮助。

于 2014-07-12T09:24:27.203 回答
0
SystemSoundID soundToPlay;

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

//playback
AudioServicesPlaySystemSound(soundToPlay);
于 2014-07-12T08:55:14.600 回答
0

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

您可以尝试使用 waitForCompletion: 直到它适合您的喜好。

于 2014-07-13T06:44:12.730 回答