我定义了一个
int x = 10;
现在我想每秒x
减少一次,直到它的 0:
if (Obstacle.activeItem == true) {
game.font.draw(game.batch, "Item active for: " + x, 100, 680);
}
我怎样才能做到这一点?
我见过人们使用 做类似的事情Class Timer
,但我不知道在这种情况下它应该是什么样子。
我试过了
int x = 10;
ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
然后
if (Obstacle.activeItem == true) {
game.font.draw(game.batch, "Item active for: " + x, 100, 680);
}
execService.scheduleAtFixedRate(new Runnable() {
public void run() {
x--;
}
}, 0L, 10L, TimeUnit.SECONDS);
但这并不像我想要的那样工作。
你用 libGdx 标记了你的问题,所以我认为你使用 libgdx。为什么不使用该update(float delta)
方法来减少计时器,而不是创建额外的 ExecutorService ?
private float timer = 10;
@Override
public void render(float delta) {
timer -= delta;
if (Obstacle.activeItem == true) {
font.draw(batch, "Item active for: " + (int)timer, 100, 680);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句