Game Development Reference
In-Depth Information
age of multithreading and nanosecond timing capabilities and 3D rendering hardware
(Prism) support.
Whenever something is changed in the Scene Graph, such as a UI control position-
ing, a CSS style definition, or an animation playing, a pulse event is scheduled and is
eventually fired to synchronize the states of elements on the Scene Graph. The trick in
JavaFX game design is to optimize pulse events so that they are focusing on the game
play logic (animation, collision detection, and so on); thus, you will minimize the other
changes (UI control location, CSS style changes, and so on) the pulse engine looks at.
You will do this by using the Scene Graph as a fixed design system, meaning that you
will use the Scene Graph to design your game structure but will not manipulate nodes
in real time on the Scene Graph, using dynamic programming logic, as the pulse sys-
tem will perform the updates.
A JavaFX pulse system allows developers to handle events asynchronously , like a
batch processing system that schedules tasks on the nanosecond level, instead of once
a day, like batch processing schedulers from the old mainframe computer days. Next,
let's examine how to schedule code in a pulse, using a .handle() method.
Harnessing the JavaFX Pulse Engine: Extending the Anima-
tionTimer Class to Generate Pulse Events
Extending the AnimationTimer class is a great way to get the JavaFX pulse system to
process your code for each pulse that it processes. Your real-time game programming
logic will be placed inside the .handle(long now) method and can be started and
stopped at will by using the other two AnimationTimer methods, .start() and .stop().
The .start() and .stop() methods are called from the AnimationTimer superclass,
although the two methods can be overridden as well; just be sure eventually to call su-
per.start() and super.stop() in your override code methods. If added as an inner class
inside your current JavaFX public void .start() method structure, the code structure
might look as follows (see Chapter 3 , Figure 3-2 ):
public void start(Stage primaryStage) {
Button btn = new Button;
new AnimationTimer() {
@Override
public void handle(long now) {
// Program logic that gets processed on every
pulse that JavaFX processes
Search WWH ::




Custom Search