我創建一個簡單的乒乓遊戲.. 現在我想設置一個計時器,將停止後,我失去和保存價值高的分數可能,但即使我設法設定了計時器並設定了他,似乎不想停下來。的iOS乒乓發展,計時器不會停止
我使用本教程中實現它: http://www.apptite.be/tutorial_ios_stopwatch.php
現在我的代碼如下所示:
- (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;
}
我計時器設置當我TapToBegin標籤上點擊.. 但是當我寬鬆或贏得計時器只是繼續運行,即使我在使用無效的功能之前,開始一個新的遊戲..(當我開始一個新的遊戲它重置)
-(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];
}
我知道教程顯示了更多的停止行爲代碼,但我嘗試了更多的選擇,我給了它所有的東西,但我認爲這是停止計時器的唯一行,因此它應該可以工作。但事實並非如此。
請大家幫忙,我有一個因在週一完成這一點,所以我嚇壞了一點點。
定時器只會在你調用'invalidate'時停止 - 就像你在'reset:'中做的那樣。當你贏或輸時,你是否在計時器上稱「無效」? – 2012-01-10 14:53:48
嗯,我想我做的..至少這是什麼,在我看來在我這裏張貼'如果(skore_hrac_hodnota
2012-01-10 15:19:43
+1製作傍代碼學習遊戲編碼而不是RPG或其他一些無法完成的超級項目。 – 2012-01-10 15:29:19