当前位置: 动力学知识库 > 问答 > 编程问答 >

loops - Delaying time using SystemcurrentTimeMillis

问题描述:

The code i'm working on deals with a flock of birds moving around fast on the canvas. What I need to do is slow down how fast the birds refresh on the screen by using the System.currentTimeMillis() method. I need to use it in a while loop so that it waits 20 milliseconds before it makes the birds move around again. I'm not quite sure on how to do this.

Here is the current code:

while(NeWorld.isAlive())

{

NeWorld.updateWorld();

}

NewWorld.isAlive allows the birds to move around, and the NeWorld.updateWorld() refreshes it. It refreshes too fast and I need to slow it down with the System.currentTimeMillis() method so that it only refreshes every 20 milliseconds.

网友答案:

If you put your updateWorld() logic in a separate function, you can call it with certain intervals. Here is an example.

boolean iShouldStillUpdateWorld = true;
while (iShouldStillUpdateWorld) {
    if (System.currentTimeMillis() % 20 == 0) {
        updateWorld();
    }
    iShouldStillUpdateWorld = checkIfIShouldUpdateWorld();
}

This simply sits in a loop and checks if 20 millis has passed. If so, it calls your updateWorld() function then checks if it should continue to repeat this process.

分享给朋友:
您可能感兴趣的文章:
随机阅读: