在tick((方法中,您不能具有 int starttime = system.nanotime((,因为它会不断对其进行更新。
我需要在tick方法中找到经过的时间,以便每2秒钟产生一个新对象。
public void tick() {
long startTime = System.nanoTime();
// wave 1
if (wave == 1) {
float k = System.nanoTime() - startTime;
/* won't work because "startTime" is constantly updating */
if (k >= 2 && k <= 3) {
handler.addObject(new BasicEnemy());
} else if (k >= 4 && k <= 5) {
handler.addObject(new BasicObject());
} else if (k >= 6 && k <= 7) {
handler.addObject(new BasicEnemy());
}
}
// wave 2
if (wave == 2) {
float k = System.nanoTime() - startTime;
/* won't work because "startTime" is constantly updating */
if (k >= 2 && k <= 3) {
handler.addObject(new BasicEnemy());
} else if (k >= 4 && k <= 5) {
handler.addObject(new BasicObject());
} else if (k >= 6 && k <= 7) {
handler.addObject(new BasicEnemy());
}
}
}
}
以上是代码的小片段。我将如何在tick方法中查找经过的时间,并重新启动其计数的每个If语句的计数?
感谢您的帮助:(
您需要在 tick()
方法之外握住 timeAtLastAccept
so:
long timeAtLastAccept = System.nanoTime();
public void tick(){
if(System.nanoTime()-timeAtLastAccept >threshold) {
//Spawn your objects!
timeAtLastAccept = System.nanoTime();
}
//do ticky things
}