我用sprite工具包製作了一個遊戲。現在我正試圖實現一些音效。我設法做了一些聲音效果,但是當我輸了的時候,我不得不實施一個聲音。所以當物體接觸地面時你會失去。但是當它發生時,它會轉換到另一個場景。所以我希望在轉換到其他場景之前播放轉換的聲音。 這是我的了:在轉換過渡時向spritekitgame添加聲音
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];
}
嗨,感謝您的幫助,我實現了它,但我完成後得到錯誤(); :函數「完成」的隱式聲明在C99中是無效的 –
@RomanoVacca我輸入錯誤。嘗試改爲'completionBlock();' – Unheilig
錯誤消失了,但它仍然會在沒有gameoversound的情況下進入其他場景。 –