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