2014-07-12 36 views
-1

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

} 

回答

1

你可以在這裏做什麼是使用完成塊。

例如:

定義一個:

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

希望這有助於。

+0

嗨,感謝您的幫助,我實現了它,但我完成後得到錯誤(); :函數「完成」的隱式聲明在C99中是無效的 –

+0

@RomanoVacca我輸入錯誤。嘗試改爲'completionBlock();' – Unheilig

+0

錯誤消失了,但它仍然會在沒有gameoversound的情況下進入其他場景。 –

0
SystemSoundID soundToPlay; 

NSString *path = [[NSBundle mainBundle] pathForResource:@"gameover1" ofType:@"wav"]; 
NSURL *pathURL = [NSURL fileURLWithPath : path]; 
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &soundToPlay); 

//playback 
AudioServicesPlaySystemSound(soundToPlay); 
+0

你的答案會如果您有關於爲什麼這是一個解決方案的一些解釋或評論來大大改善。 –

0

SKAction *sound = [SKAction playSoundFileNamed:@"fileName" waitForCompletion:NO];

您可以使用waitForCompletion實驗:直到它適合你的喜好。