-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