重复触发AVAudioPlayer时,大量fps会下降(Sprite Kit)

时间:2014-09-03 14:10:30

标签: sprite-kit avaudioplayer frame-rate

我正在使用Sprite Kit创建一个iOS游戏,你可以用它来开水枪。只要水枪被点燃,就会发出斜线声。自从我实现了这个功能以来,当使用touches-started-method重复触发声音效果时,我会遇到大量的fps问题。

有没有可能解决这个问题?

@property (nonatomic) AVAudioPlayer *splashSound;

-(id)initWithSize:(CGSize)size {

    NSError *error3;
    NSURL *splashURL = [[NSBundle mainBundle] URLForResource:@"splash" withExtension:@"caf"];
    self.splashSound = [[AVAudioPlayer alloc]initWithContentsOfURL:splashURL error:&error3];
    self.splashSound.numberOfLoops = 1;
    [self.splashSound prepareToPlay];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    startGamePlay = YES;
    if (self.waterCapacity != 0){
    waterSprayed = YES;
    [self.splashSound play]; // sound starts when screen is touched
    [self.currentWasser sprayWater];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.currentWasser removeActionForKey:@"water"];
    [self.splashSound pause]; // sound is paused when touches ended
    waterSprayed = NO;
}

2 个答案:

答案 0 :(得分:3)

使用UILongPressGestureRecognizer修复它:效果很好。真高兴!

- (void)didMoveToView:(SKView *)view
{
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(delayedSplashSound:)];
    longGesture.minimumPressDuration = 0.15;
    [view addGestureRecognizer:longGesture];
}

- (void)delayedSplashSound:(UITapGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        [self.splashSound play];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
        [self.splashSound pause];
        [self.currentWater removeActionForKey:@"water"];
        waterSprayed = NO;
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    startGamePlay = YES;
    if (self.waterCapacity != 0){
    waterSprayed = YES;
    [self.currentWater sprayWater];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.currentWater removeActionForKey:@"water"];
    waterSprayed = NO;
}

答案 1 :(得分:1)

在使用带有无限循环的AVAudioPlayer.play()AVAudioPlayer.stop()时,我还有另一种解决方案。不要拨打play()stop(),只需拨打play()一次,然后设置yourPlayerInstance.volume=0.0yourPlayerInstance.volume=1.0。这解决了我的帧毛刺/滴 - 完美的60 fps而不是下降到~40 fps。