实现游戏计时器最有效的方法是什么

时间:2022-10-22 10:38:39

I am writing an iPhone game. When the user makes his first move a timer kicks of with an interval of 0.01 seconds. A UILabel displaying the time also gets updated every time. I noticed when testing on an iPod touch 2nd gen and an iPhone 3GS that the iPod was slower (after 20 seconds the iPhone displayed 00:20,00 and the iPod displayed ~00:10,00). Is there a way to make this more reliable? If I'm correct, the NSTimer should run on its ow thread and should not be blocked by user interaction.

我正在写一款iPhone游戏。当用户第一次移动时,计时器的间隔为0.01秒。显示时间的UILabel每次都会更新。我在测试iPod touch第二代和iPhone 3GS时注意到iPod速度较慢(20秒后,iPhone显示00:20 00,iPod显示00:10 00)。有什么方法可以让它更可靠吗?如果我是对的,NSTimer应该在它的ow线程上运行,不应该被用户交互阻塞。

JNK

3 个解决方案

#1


3  

The NSTimer documentation states:

NSTimer文档:

Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs while the run loop is in a mode that is not monitoring the timer or during a long callout, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

由于典型运行循环管理的各种输入源,计时器的时间间隔的有效分辨率被限制为50-100毫秒。如果计时器的触发时间发生在运行循环处于不监视计时器的模式或长时间调用期间,那么计时器直到下一次运行循环检查计时器时才会触发。因此,计时器触发的实际时间可能是计划的触发时间之后的一段重要时间。

So accuracy can be as bad as 0.1, not 0.01 seconds. Not to mention if your thread is blocked for some reason. So if your firing time is crucial you should be looking at other things. Read this SO post for kick-off. Apple had a metronome sample code (in which, obviously, timing is crucial) but I can't find it just now.

所以准确度可以是0。1,而不是0。01秒。更不用说你的线程是否因为某种原因被阻塞了。所以如果你的射击时间很关键你应该看看其他的东西。请阅读这篇文章。苹果有一个metronome示例代码(显然,时间很重要),但我现在找不到它。

In any case, if you are implementing a timer with NSTimer, you should record your start time. Then, whenever you update your interface, simply take the difference of the current time and your start time (with NSDates).

无论如何,如果您正在使用NSTimer实现一个计时器,您应该记录您的启动时间。然后,每当更新接口时,只需计算当前时间和开始时间的差异(使用nsdate)。

#2


0  

make sure you're not basing a timer off of sleeps or delays. You should always update a timer based on things like number of clock ticks since program start or current time

确保你没有根据睡眠或延迟设置计时器。您应该根据程序启动或当前时间的时钟滴答数来更新计时器

sorry I'm not more familiar with your language

对不起,我对你的语言不太熟悉

#3


0  

You can't rely on a timer to run exactly at the specified time intervals, so the time you are displaying should always be calculated by taking time interval differences. And I doubt that a timer on the iPhone can run every 1 ms, in Quartz it is possible to get a timer call every 16 ms or so, making 60 fps - so scheduling it at 1ms probably means "run as soon as possible", which might be quite different on different hardware.

您不能依赖于计时器在指定的时间间隔运行,所以您显示的时间应该总是通过间隔时间间隔来计算。我怀疑每1毫秒计时器在iPhone上运行,在石英可以得到一个计时器调用每16毫秒左右,使60 fps,所以安排在1 ms可能意味着“尽快”,这可能是非常不同的在不同的硬件上。

#1


3  

The NSTimer documentation states:

NSTimer文档:

Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs while the run loop is in a mode that is not monitoring the timer or during a long callout, the timer does not fire until the next time the run loop checks the timer. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time.

由于典型运行循环管理的各种输入源,计时器的时间间隔的有效分辨率被限制为50-100毫秒。如果计时器的触发时间发生在运行循环处于不监视计时器的模式或长时间调用期间,那么计时器直到下一次运行循环检查计时器时才会触发。因此,计时器触发的实际时间可能是计划的触发时间之后的一段重要时间。

So accuracy can be as bad as 0.1, not 0.01 seconds. Not to mention if your thread is blocked for some reason. So if your firing time is crucial you should be looking at other things. Read this SO post for kick-off. Apple had a metronome sample code (in which, obviously, timing is crucial) but I can't find it just now.

所以准确度可以是0。1,而不是0。01秒。更不用说你的线程是否因为某种原因被阻塞了。所以如果你的射击时间很关键你应该看看其他的东西。请阅读这篇文章。苹果有一个metronome示例代码(显然,时间很重要),但我现在找不到它。

In any case, if you are implementing a timer with NSTimer, you should record your start time. Then, whenever you update your interface, simply take the difference of the current time and your start time (with NSDates).

无论如何,如果您正在使用NSTimer实现一个计时器,您应该记录您的启动时间。然后,每当更新接口时,只需计算当前时间和开始时间的差异(使用nsdate)。

#2


0  

make sure you're not basing a timer off of sleeps or delays. You should always update a timer based on things like number of clock ticks since program start or current time

确保你没有根据睡眠或延迟设置计时器。您应该根据程序启动或当前时间的时钟滴答数来更新计时器

sorry I'm not more familiar with your language

对不起,我对你的语言不太熟悉

#3


0  

You can't rely on a timer to run exactly at the specified time intervals, so the time you are displaying should always be calculated by taking time interval differences. And I doubt that a timer on the iPhone can run every 1 ms, in Quartz it is possible to get a timer call every 16 ms or so, making 60 fps - so scheduling it at 1ms probably means "run as soon as possible", which might be quite different on different hardware.

您不能依赖于计时器在指定的时间间隔运行,所以您显示的时间应该总是通过间隔时间间隔来计算。我怀疑每1毫秒计时器在iPhone上运行,在石英可以得到一个计时器调用每16毫秒左右,使60 fps,所以安排在1 ms可能意味着“尽快”,这可能是非常不同的在不同的硬件上。