如何使用 ntimer 显示秒?

发布于 2024-11-10 06:09:22 字数 120 浏览 4 评论 0原文

我正在做一个游戏项目。我需要知道如何显示游戏开始到游戏结束的秒数?还需要以“00:01”的格式显示。另外,如果时间超过 60 分钟,还应该显示时间“1:00:01”,

有什么指导吗?

谢谢...

I am working on a game project. I need to know that how can I display the seconds when game is started to the end of game ? also need to display in format of " 00:01 " . additional if time goes to beyond the 60 min than it should also display the hours " 1:00:01 "

any guidance ?

Thanks...

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

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

发布评论

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

评论(3

苍风燃霜 2024-11-17 06:09:22

结合内森和马克的答案后,完整的计时器方法可能如下所示:

- (void)timer:(NSTimer *)timer {
    NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];

    NSInteger seconds = secondsSinceStart % 60;
    NSInteger minutes = (secondsSinceStart / 60) % 60;
    NSInteger hours = secondsSinceStart / (60 * 60);
    NSString *result = nil;
    if (hours > 0) {
        result = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    }
    else {
        result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];        
    }
    // set result as label.text
}

当您开始游戏时,您设置开始日期并启动计时器,如下所示:

self.startDate = [NSDate date];
timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];

当停止游戏时,您使用以下内容:

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

After combining the answers of Nathan and Mark the complete timer method could look something like this:

- (void)timer:(NSTimer *)timer {
    NSInteger secondsSinceStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];

    NSInteger seconds = secondsSinceStart % 60;
    NSInteger minutes = (secondsSinceStart / 60) % 60;
    NSInteger hours = secondsSinceStart / (60 * 60);
    NSString *result = nil;
    if (hours > 0) {
        result = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
    }
    else {
        result = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];        
    }
    // set result as label.text
}

when you start the game you set the startDate and start the timer like this:

self.startDate = [NSDate date];
timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];

when stopping the game you use this:

self.startDate = nil;
[timer invalidate];
timer = nil;
无边思念无边月 2024-11-17 06:09:22

您可以安排一个每秒触发一次的重复计时器,当它触发时,它会调用一个更新时间显示的方法:

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

然后在更新方法中,

- (void)timerFireMethod:(NSTimer*)theTimer {
    //remove a second from the display
}

您需要将计时器设置为一个属性,以便在完成时它可以失效。

You can schedule a repeating timer that fires every second, when it fires it calls a method that updates your time display:

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

Then in the update method

- (void)timerFireMethod:(NSTimer*)theTimer {
    //remove a second from the display
}

You'll want to set the timer to an attribute so that it can be invalidated when finished.

鸢与 2024-11-17 06:09:22

NSTimeInterval t = 10000;
printf( "%02d:%02d:%02d\n", (int)t/(60*60), ((int)t/60)%60, ((int)t)%60 );

输出
02:46:40

如果你想获得带小数点的秒,那么你必须使用 mode() 有点困难。

NSTimeInterval t = 10000;
printf( "%02d:%02d:%02d\n", (int)t/(60*60), ((int)t/60)%60, ((int)t)%60 );

outputs
02:46:40

if you want to get the seconds with decimal points then it is a little more difficult you have to use mode().

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