2014-08-30 5 views
1

gameScene.m에서 게임을하는 동안 skspritenode로 음악을 켜거나 끌 수있는 방법은 다음과 같습니다. 나는 완벽하게 소리를 낼 수 있지만 사용자가 원할 때마다 켜고 싶습니다. 게임에서 소리를 켜거나 끄고 자신의 ipad를 재생합니다.spritekit 게임에서 사운드 효과 on/off

//.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 
+0

[_backgroundAudioPlayer pause]로 음악을 일시 중지하거나 [_backgroundAudioPlayer stop]으로 음악을 중지 할 수 있습니다. – 0x141E

+0

나는이 놀이를 알고, 멈추고 멈춘다. 그러나 나는 동시에 멈추게하는 나의 3 개의 소리 모두를 on/off하고 싶다? – anjani

+0

게임이나 사운드 효과에서 배경 음악을 재생하려고합니까? – 0x141E

답변

1

게임에 음향 효과를 추가하려면 AVAudioPlayer 대신 playSoundFileNamed SKAction을 사용하는 것이 좋습니다. 여기에 그 작업을 수행하는 방법의 예

@property BOOL playSounds; 

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

// Play sound effect only if playSounds is set 
if (_playSounds) { 
    [self runAction:_playJumpSound]; 
} 

편집 : 추가 메소드로 들린다. _playSounds == YES 인 경우에만 사운드가 재생됩니다.

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

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

시도해 보셨습니까? – 0x141E

+0

잘 작동하지만 버튼을 사용하여 모든 사운드를 멈추고 모든 사운드를 재생하려면 ON으로 설정하고 재생하려면 켜기/끄기 버튼에 대해 걱정합니다. – anjani

+0

수정 된 답변보기 – 0x141E

관련 문제