Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios spritekit游戏中的音效开/关_Ios_Objective C_Iphone_Ios7_Sprite Kit - Fatal编程技术网

Ios spritekit游戏中的音效开/关

Ios spritekit游戏中的音效开/关,ios,objective-c,iphone,ios7,sprite-kit,Ios,Objective C,Iphone,Ios7,Sprite Kit,在游戏场景中。m我们如何在玩游戏时使用skspritenode制作开/关音乐,使用下面的代码,我可以完美地播放声音,但我希望在用户想要在游戏中打开或关闭声音以玩自己的ipad时打开/关闭 //.h file #import <SpriteKit/SpriteKit.h> @interface MyScene : SKScene //.m file #import "MyScene.h" #import <AVFoundation/AVAudioPlayer.h> @i

在游戏场景中。m我们如何在玩游戏时使用skspritenode制作开/关音乐,使用下面的代码,我可以完美地播放声音,但我希望在用户想要在游戏中打开或关闭声音以玩自己的ipad时打开/关闭

//.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
/.h文件
#进口
@界面MyScene:SKScene
//.m文件
#导入“MyScene.h”
#进口
@接口MyScene()
@结束
@进口基金会;
@MyScene的实现
{AVAudioPlayer*\u AudioPlayer;}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
如果(_gameLayer.speed>0){
//用于驾驶飞机
_playerPlane.physicsBody.velocity=CGVectorMake(0,0);
[\u playerPlane.physicsBody applyImpulse:CGVectorMake(0,14)];
[自跳声音];
}
-(无效)didBeginContact:(SKPhysicsContact*)联系人{
_gameLayer.speed=0;
[自我反省];
skspritenodeGameOver.hidden=否;
[self-hitSound];}
-(空)跳跃声
{
NSURL*文件=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@“jump”of type:@“wav”]];
_AudioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:文件错误:nil];
[_AudioPlayer设置音量:0.5];
[音频播放器播放];
}
-(空)hitsound
{
NSURL*文件=[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@“jump”of type:@“wav”]];
_AudioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:文件错误:nil];
[_AudioPlayer设置音量:0.5];
[音频播放器播放];
}
@结束

若要在游戏中添加音效,我建议使用名为SKAction的PlaySoundFile,而不是AVAudioPlayer。下面是一个示例:

@property BOOL playSounds;

// Define action to play a sound effect
_playJumpSound = [SKAction playSoundFileNamed@"jump.wav" waitForCompletion:NO];

// Play sound effect only if playSounds is set
if (_playSounds) {
  [self runAction:_playJumpSound];
}
编辑:向方法添加声音。只有当_playSounds==是时,声音才会播放

- (void) playJumpSound
{
    if (_playSounds) {
        [self runAction:_playJumpSound];
    }
}

- (void) playHitSound
{
    if (_playSounds) {
        [self runAction:_playHitSound];
    }
}

您可以使用[\u backgroundAudioPlayer pause]暂停音乐,也可以使用[\u backgroundAudioPlayer stop]停止音乐。我知道该播放,暂停并停止,但我想同时打开/关闭所有3种声音?您是否尝试在游戏中播放背景音乐或音效?音效仅像跳跃、命中、点和崩溃,但如果用户想静音/关闭这些声音,则我需要类似的开/关选项。其工作正常,但我担心开/关按钮w播放时,我想打开以播放所有声音,关闭以使用按钮停止所有声音请参阅我编辑的答案。您是在询问如何创建按钮,还是该按钮将如何打开/关闭声音?按钮将如何打开/关闭声音如果按钮打开,则设置_playSound=是,否则设置_playSound=否。