以下功能连续运行:
void MyClass::handleTriggered(float dt)
{
// dt is time interval (period) between current call to this function and previous one
}
dt
类似于0.0166026
秒(每秒60帧)。
我打算每0.25
秒执行一次操作。目前,我使用的事实是我们每秒60帧,即函数调用每秒发生60次:
static long int callCount = 0;
++callCount;
// Since we are going with 60 fps, then 15 calls is 1/4 i.e. 0.25 seconds
if (callCount % 15 == 0)
// Do something every 0.25 seconds (every 15 calls)
现在,我想知道如何使用float
类型而不是int
类型进行另一种操作:
static float sumPeriod = 0.0;
// Total time elapsed so far
sumPeriod += dt;
if (/* How to compose the condition? */) {
// Every 0.25 seconds do something
}
答案 0 :(得分:1)
您必须将dt
的总和相加,当它们达到0.25时,您需要从总和中减去0.25来做您的事情。
static float sum = 0.0;
sum += dt;
if(sum > 0.25) {
sum -= 0.25;
// Do what you want to do every 0.25 secs here.
}
答案 1 :(得分:-1)
static float sumPeriod = 0.0;
// Total time elapsed so far
sumPeriod += dt;
if (sumPeriod > 0.25) {
sumPeriod -= 0.25 * int(sumPeriod / 0.25);
// Every 0.25 seconds do something
}