0

**Many Thanks people! I used jeffaphone and Martin H's answers, Been staring at this for hours thank you so much :)**

I'm working on a project where I have a short sound file that plays every 1.35 seconds when the game is running but when the game stops how do I turn off the timer? If you do nothing the sound file continues to loop and a new loop will begin if you start the game again etc.

The code I have so far is this;

if ((self.currentState = GameStateRunning)) {

self.timer = [NSTimer scheduledTimerWithTimeInterval:1.35 target:self selector:@selector(sound:) userInfo:nil repeats:YES];
 //   [self sound:nil];

}
else if ((self.currentState = GameStateEnded)) {
     [sound removeAllActions];

I've tried stopping the sound but that doesn't seem to work so is there a way to stop or reset the timer so the loop will stop until the game starts up again?

John B
  • 105
  • 1
  • 2
  • 11
  • 2
    [self.timer invalidate] – Gruntcakes Feb 24 '14 at 22:09
  • Instead of editing your question to say which answer you used, select the check mark next to that answer. This is how that the user is reward reputation for their correct answer, and subsequent viewers can identify the solution that was used. – Patrick Goley Feb 24 '14 at 23:22

1 Answers1

1

To stop a timer:

[self.timer invalidate];
self.timer = nil;

Note that you don't have a retain count, so you don't release it (if you're non-ARC).

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222