I made a simple game in Xcode where the character moves around the screen to collect coins, avoiding stones.
For traffic, I use NSTimers, which work fine. The problem occurs when I add sound effects. I use AVAudioPlayers for this and it is a little behind in the game. I commented on them to find out if they caused a problem, and it seems to be so. I looked around here on Qaru and other sites, but I can not find a good answer. Any help appreciated!
code:
- (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; }
source share