-1

I recently switched from swing to javaFX and I noticed that everyone is using AnimationTimer to make a game loop. And my question is is there a performance advantage in using AnimationTimer over just using self made threads to run the game loop on ? Because so far in swing it was ok to just create threads that handle the render and update of the game but no one seems to do this in javaFX so I would like to know whether using self made threads can cause any performance issues.

Thanks in advance !

Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
  • 1
    It's probably bad practice, but we'd need to see code. JavaFX and Java both have restrictions which threads should be accessing any GUI objects that display on the screen. If you're not doing this properly bad stuff can happen. In Java, `javax.swing.Timer` does this properly for you, and I'm guessing JavaFX an AnimationTimer might preform the same task. – markspace Dec 31 '21 at 17:17
  • You could improve this question by using an example with AnimationTimer. You've almost setup some straw men arguments. "in swing it was ok to just create threads" do you have some context? Do you have a performance goal? – matt Dec 31 '21 at 17:19
  • 6
    I would ask the oposite question. If everybody who knows what he is doing uses the AnimationTimer class what is your motivation then to even think about a different solution? What improvement do you expect from such a deviation from common practice? – mipa Dec 31 '21 at 17:57

1 Answers1

1

The thing about AnimationTimer is that it typically will run at the display refresh rate and execute on the JavaFX platform thread so it is ideal for updating the game UI.

However, running all your game logic on the UI thread may not be the best. It depends a great deal on your game and it's requirements. I would consider running a background thread that computes the next game state and then only doing the rendering aspects of that in the AnimationTimer callback. There of course will be added complexity and synchronization needed, so you need to decide if this makes sense in your specific case.

swpalmer
  • 3,890
  • 2
  • 23
  • 31
  • See also this `AnimationTimer` [example](https://stackoverflow.com/a/69608238/230513) and this `Task` animation [example](https://stackoverflow.com/a/44056730/230513). – trashgod Jan 01 '22 at 19:30