在gameScene.m中,我們如何在玩遊戲時使用skspritenode打開/關閉音樂,使用下面的代碼我可以完美地播放聲音,但是每當用戶想要時打開或關閉遊戲中的聲音以播放自己的ipad。spritekit遊戲中的聲音效果開啓/關閉
//.h file
#import <SpriteKit/SpriteKit.h>
@interface MyScene : SKScene
//.m file
#import "MyScene.h"
#import <AVFoundation/AVAudioPlayer.h>
@interface MyScene() <SKPhysicsContactDelegate>
@end
@import AVFoundation;
@implementation MyScene
{AVAudioPlayer *_AudioPlayer;}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (_gameLayer.speed > 0) {
//for flying plane
_playerPlane.physicsBody.velocity = CGVectorMake(0, 0);
[_playerPlane.physicsBody applyImpulse:CGVectorMake(0, 14)];
[self jumpsound];
}
-(void)didBeginContact:(SKPhysicsContact *)contact{
_gameLayer.speed = 0;
[self removeAllActions];
skspritenodeGameOver.hidden = NO;
[self hitSound];}
- (void)jumpsound
{
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"jump" ofType:@"wav"]];
_AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[_AudioPlayer setVolume:0.5];
[_AudioPlayer play];
}
- (void)hitsound
{
NSURL *file = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"jump" ofType:@"wav"]];
_AudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file error:nil];
[_AudioPlayer setVolume:0.5];
[_AudioPlayer play];
}
@end
您可以用[_backgroundAudioPlayer暫停]暫停音樂,或用[_backgroundAudioPlayer停止]停止音樂。 – 0x141E 2014-08-30 16:43:56
我知道這個戲,暫停和停止,但我想打開/關閉所有我的3種聲音同時停止? – anjani 2014-08-30 17:06:01
您是否想要在遊戲或音效中播放背景音樂? – 0x141E 2014-08-30 18:30:24