在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