ios - 在游戏结束过渡时为 spritekitgame 添加声音

标签 ios xcode audio sprite-kit skscene

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

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

}

最佳答案

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

例如:

定义一个:

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

希望这可以帮助。

关于ios - 在游戏结束过渡时为 spritekitgame 添加声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24711020/

相关文章:

ios - UIWebView 中的自定义 CSS

android - 尝试从终端构建时在 ubuntu 上构建 cordova 错误

ios - swift:导航栏模糊效果

iphone - 有没有其他方法可以在不越狱的情况下查看 iOS 应用程序的大小?

Swift 3 单元测试 - 使用 import @testable 时链接器失败

python - 如何将系统音频抓取到python?

c++ - 无法在GST-launch-1.0的Qemu上在armv7上播放WAV音频

xcode - 在 Xcode 中删除用户代码片段

ios - 无法将类型 'Authorize.ClubsViewController' (0x109c70cc8) 的值转换为 'Authorize.NewViewController' (0x109c70df8)

audio - 录制立体声混音和寄生虫