I am creating a simple pong game .. right now I wanted to set a timer that will stop after I lose and maybe keep the value until high grade, but although I managed to set the timer and set it up, it doesn't seem to want stop.
I implemented it using this tutorial: http://www.apptite.be/tutorial_ios_stopwatch.php
Now my code is as follows:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { if (StavHry == StavHryPozastaven) { TapToBegin.hidden = YES; StavHry = StavHryAktivni; } else if (StavHry == StavHryAktivni) { [self touchesMoved:touches withEvent:event]; } startDate = [NSDate date]; stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer)userInfo:nil repeats:YES]; } - (void)updateTimer { static NSInteger counter = 0; StopWatchLabel.text = [NSString stringWithFormat:@"Counter: %i", counter++]; NSDate *currentDate = [NSDate date]; NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm:ss.SSS"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; NSString *timeString=[dateFormatter stringFromDate:timerDate]; StopWatchLabel.text = timeString; }
i is set to a timer when I click on the TapToBegin label. but when I lost or won the timer, it just keeps working, even though I used invalidate in the function before starting a new game. (when I start a new game that he resets)
-(void)reset:(BOOL) novahra { //funkce reset self.StavHry = StavHryPozastaven; mic.center = self.view.center; if(novahra) { if(skore_hrac_hodnota < skore_pc_hodnota){ TapToBegin.text = @"Protivnik Vyhrál, smůla!"; [stopWatchTimer invalidate]; } else { TapToBegin.text = @"Vyhráls! Gratulujem!"; [stopWatchTimer invalidate]; } skore_hrac_hodnota = 0; skore_pc_hodnota = 0; } else { self.StavHry = StavHryAktivni; //TapToBegin.text = @"Pokračuj!"; } skore_hrac.text = [NSString stringWithFormat:@"%d", skore_hrac_hodnota]; skore_pc.text = [NSString stringWithFormat:@"%d", skore_pc_hodnota]; }
I know that the tutorial shows more lines of code to stop the action, but I tried more options that I gave it everything that it has, but I think this is the only line that stops the timer so that it works. but this is not so.
Please help, I have the opportunity on Monday to finish this, so I'm a little worried.
source share