如何使用 ntimer 显示秒?
我正在做一个游戏项目。我需要知道如何显示游戏开始到游戏结束的秒数?还需要以“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
结合内森和马克的答案后,完整的计时器方法可能如下所示:
当您开始游戏时,您设置开始日期并启动计时器,如下所示:
当停止游戏时,您使用以下内容:
After combining the answers of Nathan and Mark the complete timer method could look something like this:
when you start the game you set the startDate and start the timer like this:
when stopping the game you use this:
您可以安排一个每秒触发一次的重复计时器,当它触发时,它会调用一个更新时间显示的方法:
然后在更新方法中,
您需要将计时器设置为一个属性,以便在完成时它可以失效。
You can schedule a repeating timer that fires every second, when it fires it calls a method that updates your time display:
Then in the update method
You'll want to set the timer to an attribute so that it can be invalidated when finished.
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().