通过Sprite Kit场景播放音乐?

时间:2014-06-13 02:15:23

标签: xcode sprite-kit scene background-music

所以我有一个精灵套装游戏,我正在添加音乐。音乐工作正常,直到我决定添加滑块来控制音乐音量。为此,我为"选项菜单添加了另一个场景。"问题是,当我退出场景去实际的游戏界面时,游戏音乐完全消失了。我也无法删除实际的滑块。我的选项场景代码将在下面列出。 (它还在制作中)我是这个网站的新手,所以如果代码搞砸了我很抱歉。

@interface OptionsScene ()
@property BOOL contentCreated;
@end

@implementation OptionsScene

-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
    self.anchorPoint = CGPointMake(0.5, 0.5);

}
return self;
}

-(void)didMoveToView:(SKView *)view
{
    NSString *music = [[NSBundle mainBundle] pathForResource:@"InAmberClad" ofType:@"mp3"];
    backGroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
    backGroundMusic.delegate = self;
    backGroundMusic.numberOfLoops = -1;
    [backGroundMusic play];



CGRect frame = CGRectMake(200, 300, 150, 10);
_volumeSlider = [[UISlider alloc] initWithFrame:frame];
[_volumeSlider addTarget:self action:@selector(volumeControl) forControlEvents:UIControlEventValueChanged];
_volumeSlider.maximumValue = 1;
_volumeSlider.minimumValue = 0;
_volumeSlider.continuous = YES;
[_volumeSlider setValue:0.5];
[_volumeSlider setBackgroundColor:[UIColor blackColor]];

if (!self.contentCreated) {
    [self.view addSubview:_volumeSlider];
    [self createContents];
    self.contentCreated = YES;
}
}

-(void)createContents
{
    self.scaleMode = SKSceneScaleModeAspectFill;
}

-(void)volumeControl
{
[backGroundMusic setVolume:_volumeSlider.value];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
SKScene *options = [[MyScene alloc] initWithSize:self.size];
[self.view presentScene:options];


}

@end

1 个答案:

答案 0 :(得分:0)

我认为如果你不将backgroundMusic委托给自己,这个问题就会解决。此外,在播放音乐之前,运行[backGroundMusic prepareToPlay];将提高性能。

这是指向教程的链接,在页面搜索"音乐"看看他是怎么做到的。

Ray Wenderlich