Game Development Reference
In-Depth Information
Both this and the previous level are what would be considered the top-level approaches
and would be best applied to games that are static yet animated, or that have a less dy-
namic game play.
The middle-level solution is to use the Timeline class and its related KeyFrame
and KeyValue classes, which are great for implementing the type of timeline-based an-
imation that you see in drag-and-drop tools, such as Flash. As you will find, if you look
online at JavaFX game engine discussions, this is a popular approach, as many anima-
tions are implemented by creating a single KeyFrame object and then using a TimeLine
object to process the pulse events.
Using the Timeline object approach allows you to specify a frame rate for process-
ing your game loop, such as 30FPS. This would be appropriate for less dynamic games
that can use a lower frame rate, because they do not involve a lot of interframe game
processing, such as sprite movement, sprite animation, collision detection, or physics
calculation. It is important to note that if you use a Timeline object (class), you will be
defining variables in system memory for frame rate and at least one KeyFrame object
reference (these are part of the Timeline class definition) as well as properties (vari-
ables) inherited from the Animation superclass, such as status , duration , delay ,
cycleCount , cycleDuration , autoReverse , currentRate , currentTime , and an onFin-
ished (ActionEvent) ObjectProperty.
If you are familiar with creating animations, you will see that Timeline, along with
at least one KeyFrame object and potentially a large number of KeyValue objects
stored inside each KeyFrame object, is clearly designed for (optimized toward) creat-
ing timeline-based animation. Although this is a very powerful feature, it also means
that using a Timeline and KeyFrame object for game loop processing will create close
to a dozen areas of memory allocation that you may not even use in your game or that
may not be designed (coded) optimally for your game design implementation.
Fortunately, there is another javafx.animation package timing-related class that car-
ries none of this prebuilt class overhead, and so I term this the lowest-level approach,
in which you have to build all your game processing logic yourself, inside one simple
.handle() function, which accesses the JavaFX pulse engine on every pass it makes.
The low-level solution involves using the AnimationTimer class, so named be-
cause Java (Swing) already has a Timer class (javax.swing.Timer), as does Java's util-
ity class (java.util.Timer), which you could also use if you were an advanced enough
programmer to deal with all the thread synchronization issues (and coding).
Because this is a beginner-level book, you will stick with looping your game using
the Java 8 game engine (JavaFX 8). JavaFX has its own Timer class, in the
javafx.animation package, called AnimationTimer so as not to cause confusion with
Swing GUI Toolkit's Timer class (which is still supported, for legacy code reasons).
Search WWH ::




Custom Search