【发布时间】:2014-09-03 14:10:29
【问题描述】:
我正在使用 Sprite Kit 创建一个 iOS 游戏,您可以在其中发射水枪。只要水枪开火,就会播放一个斜线声。由于我实现了这个功能,当使用 touches-began-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;
}
【问题讨论】:
-
为什么音效应该一直播放的时候循环数是1?如果它是一个长音频文件,您应该将其更改为一个小文件(1-5 秒)并简单地循环播放。 PS:如果你不使用错误对象,你可以在 init 方法中传入 error:nil 。
-
这个在真机上测试过吗?模拟器很糟糕。
-
感谢您的回复。不幸的是,这种变化没有任何区别。我想我会使用 UILongPressGestureRecognizer 以便在触摸屏幕 1 秒后播放声音文件。我想我可以通过这种方式防止低帧率
标签: sprite-kit avaudioplayer frame-rate