AVAudioPlayer导致我的游戏落后 [英] AVAudioPlayer causes my game to lag
问题描述
我用Xcode开发了一个简单的游戏,角色在屏幕上移动以收集硬币,同时避免被石头击中.
I've made a simple game in Xcode where the character moves around the screen to collect coins, while avoiding getting hit by rocks.
对于运动,我使用了很好的NSTimers.当我添加声音效果时,会出现问题.为此,我使用了AVAudioPlayers,它使游戏滞后了一点.我将它们注释掉,以查看它们是否引起了问题,情况似乎是这样.我在Stack Overflow和其他站点上到处都是,但是似乎找不到一个好的答案.任何帮助表示赞赏!
For the movement I use NSTimers which work fine. The problem occurs when I add sound effects. I use AVAudioPlayers for this and it makes the game lag a little bit. I commented them out to see if they caused the problem, and it seems to be the case. I've looked around here on Stack Overflow and on other sites, but I can't seem to find a good answer. Any help appreciated!
代码:
- (void)CharacterMoving {
if (CGRectIntersectsRect(Character.frame, Rock.frame)) {
[self performSelector:@selector(GameOver) withObject:nil afterDelay:0.35];
[Thousand play];
}
- (IBAction)Start:(id)sender {
NSString *soundEffect=[[NSBundle mainBundle]pathForResource:@"1000" ofType:@"mp3"];
Thousand=[[AVAudioPlayer alloc]initWithContentsOfURL: [NSURL fileURLWithPath:soundEffect] error:NULL];
Thousand.numberOfLoops=-0;
}
推荐答案
我知道了.我使用系统声音服务而不是AVAudioPlayer,现在它可以正常运行.
I figured it out. I used System Sound Services instead of AVAudioPlayer, and now it works perfectly.
这篇关于AVAudioPlayer导致我的游戏落后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!