0

我必须处理一些对象的动画。现在我已经用 4 种方法完成了:getAnimationCooldown()resetAnimationCooldown()和.subAnimationCooldown()nextFrame()

但可以肯定的是,有更简单的方法可以做到这一点。我想nextFrame()每 100 毫秒调用一次方法。是否有捷径可寻?我知道ScheduledExecutorTimer但我看到他们为这个任务创建了新线程,我需要为这个for()循环中的每个对象循环调用这个方法。那么有没有一些简单的方法可以称之为:

for (Object object : objects) {
 every(100, TimeUnit.MILISECONDS) {
  object.nextFrame();
 }
}
4

1 回答 1

1

A couple ideas are:

First: Is it possible to invert the requirement to call nextFrame every 100ms? If so, it could become "Call nextFrame as often as you can, & the object will update appropriately for the amount of time since its last nextFrame"? You'd still want to call it often enough, but this would remove the pressure to call nextFrame at precise 100ms intervals.

With this idea, your main loop would deal with each of the actions you mention, like this:

while(!isGameOver()) {
  moving();
  shooting();
  for (Object object : objects) {
    object.nextFrame();
  }
}

Second: Switch to a fully event-driven model, but beware the "inside out logic" danger of that model.

于 2013-02-09T12:40:14.543 回答