我在LibGDX中开发游戏,我想每隔ms调用一次更新函数.
但我不知道如何在我的情况下做到 -
while(gameLoop) {
renderWorld();
}
public void renderWorld() {
// Some rendering code here
if(world.map[mapPos].ID == 9) {
updateWater(mapPos); // This function makes the water animate, but i must put a time limit otherwise it will be too hard to see the animation, how can i limit this?
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到我想要更新水,我不能没有时间限制这样做,因为否则水"动画"将太快甚至看不到.
在我看来,有一个更好的方法来解决这个问题.由于您总是按增量时间更新所有内容,因此您可以简单地总结时间.如果时间超过500毫秒,则更新它并将计时器重置为0.因为更新并不总是精确到500毫秒,但如果您的逻辑不在60帧以下,您就不会注意到它.
我不会使用线程或计时器.
这是一个quic的例子:
@Override
public void act(float delta) {
// sum the deltas
sum += delta;
// time to update?
if (sum >= update_time){
//update the map here and set the update_time to the 500ms for example
}
}
Run Code Online (Sandbox Code Playgroud)
这将是一种不使用Timer类并使用常规libgdx"系统"的计时器.在这种情况下,Map可以是一个Actor,你可以通过重写act方法来定期更新它,如上所示.(Stage-Actor-System来自Sceen2Dlibgdx 的框架)像往常一样调用法案public void render(float delta) {...}.
如果你想使用timer,请查看libgdx中的Timerclass.链接到计时器