我用精灵套件制作了一款游戏。现在我正在尝试执行一些音效。我设法做了一些音效,但我被困在我必须执行声音的部分,无论何时你输了。所以当物体接触地面时,你就输了。但当这种情况发生时,它会转换到另一个场景。所以我希望在过渡到其他场景之前播放游戏音效。这是我得到的:
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];
}
你可以在这里使用补全块。
例如:定义一个:
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];
}];
}
SystemSoundID soundToPlay;
NSString *path = [[NSBundle mainBundle] pathForResource:@"gameover1" ofType:@"wav"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &soundToPlay);
//playback
AudioServicesPlaySystemSound(soundToPlay);
SKAction *sound = [SKAction playSoundFileNamed:@"fileName" waitForCompletion:NO];
您可以尝试使用waitForCompletion:,直到它适合您。