AVAudioPlayer导致我的游戏滞后

时间:2014-11-30 21:07:29

标签: xcode avaudioplayer

我在Xcode中制作了一个简单的游戏,角色在屏幕上移动以收集硬币,同时避免被岩石击中。

对于运动我使用NSTimers工作正常。添加声音效果时会出现问题。我使用AVAudioPlayers,这让游戏有点滞后。我评论他们是否引起了问题,而且似乎就是这种情况。我在Stack Overflow和其他网站上看了一下,但我似乎无法找到一个好的答案。任何帮助表示赞赏!

代码:

- (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; 
}

1 个答案:

答案 0 :(得分:1)

我明白了。我使用System Sound Services而不是AVAudioPlayer,现在它可以很好地工作。

这非常有用:https://developer.apple.com/library/mac/documentation/AudioToolbox/Reference/SystemSoundServicesReference/index.html