我正试图使代码每X秒重复运行一次。
让我画一个摔跤的场景。
我已经写了一行代码,在比赛开始40秒时给泰森5公斤的一拳,然后停止…但我不可能在40秒内用5公斤重的一拳赢得比赛。我需要能够保持每40秒打卡一次。
请问谁知道我怎么能做到这一点?
PS。
{
real punch;
real time = CURRENT_TIME;
real tau = 40;
if (time<=tau)
{
punch=5;
}
else
{
punch=0;
}
return punch;
}
在可能获取时间的场景中,我可以看到您使用
当前时间
你可以这样做:
static int lastTimeExecuted = 0; // Using int here, but you should mach type of CURRENT_TIME
// See how much time passed since last execution
if ((CURRENT_TIME - lastTimeExecuted) >= 40)
{
punch = 5;
lastTimeExecuted = CURRENT_TIME ;
}
else
{
punch = 0;
}
请记住,上面的代码应该在一个循环中。