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
[_backgroundAudioPlayer pause]로 음악을 일시 중지하거나 [_backgroundAudioPlayer stop]으로 음악을 중지 할 수 있습니다. – 0x141E
나는이 놀이를 알고, 멈추고 멈춘다. 그러나 나는 동시에 멈추게하는 나의 3 개의 소리 모두를 on/off하고 싶다? – anjani
게임이나 사운드 효과에서 배경 음악을 재생하려고합니까? – 0x141E