NSTimer 变得更快

发布于 2024-11-19 20:28:52 字数 1307 浏览 1 评论 0原文

我正在尝试使用计时器来跟踪游戏中的时间。这是计时器的代码。为什么每次我从主菜单重新启动游戏时,计时器都会快两倍。是因为我在退出之前没有使计时器失效吗?我试图使计时器无效,但它只是给出了错误 exc_bad access

-(void) StartTimer {
   TotalSeconds = 0;
   GameCenterTotalSeconds = 0;
   timeSec = 0;
   timeMin = 0;
   timer = [[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]retain];
   //[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer*) timer {        
TotalSeconds++;
    GameCenterTotalSeconds= GameCenterTotalSeconds + .1;
    timeSec = timeSec + .1;
    if (timeSec >= 60)
    {
        timeSec = 00;
        timeMin++;
    }
    //Format the string 00:00
    NSString* timeNow = [NSString stringWithFormat:@"Time: %02d:%.1f", timeMin, timeSec];
    //Display on your label
    timeLabel.text = timeNow;
}

//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer {
    [timer invalidate];

    //Since we reset here, and timerTick won't update your label again, we need to refresh it again.
    //Format the string in 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
    //Display on your label
    timeLabel.text = timeNow;
}

I am trying to use to timer to keep track of a time in a game. Here is the code for the timer. How come every time I relaunch the game from the main menu the timer gets almost twice as fast. Is it because i am not invalidating the timer before I quit. I tried to invalidate the timer but it just gives the error exc_bad access

-(void) StartTimer {
   TotalSeconds = 0;
   GameCenterTotalSeconds = 0;
   timeSec = 0;
   timeMin = 0;
   timer = [[NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]retain];
   //[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer*) timer {        
TotalSeconds++;
    GameCenterTotalSeconds= GameCenterTotalSeconds + .1;
    timeSec = timeSec + .1;
    if (timeSec >= 60)
    {
        timeSec = 00;
        timeMin++;
    }
    //Format the string 00:00
    NSString* timeNow = [NSString stringWithFormat:@"Time: %02d:%.1f", timeMin, timeSec];
    //Display on your label
    timeLabel.text = timeNow;
}

//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer {
    [timer invalidate];

    //Since we reset here, and timerTick won't update your label again, we need to refresh it again.
    //Format the string in 00:00
    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
    //Display on your label
    timeLabel.text = timeNow;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年里的时光 2024-11-26 20:28:52

如果您在退出之前没有使计时器无效,那么下次启动时您将创建第二个计时器,因此事件触发的频率是原来的两倍。

If you don't invalidate the timer before you exit, the next time you start you are creating a second timer, so events are firing twice as often.

温暖的光 2024-11-26 20:28:52

只是使计时器无效,然后创建一个新的更快的计时器

just invalidate the timer, and create a new faster one

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文