28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 ■ The Foundation of Game Play Loop: The <strong>Java</strong>FX Pulse System and the Game Processing Architecture<br />

Using the Timeline object approach allows you to specify a frame rate for processing your game loop, such<br />

as 30FPS. This would be appropriate for less dynamic games that can use a lower frame rate, because they do not<br />

involve a lot of interframe game processing, such as sprite movement, sprite animation, collision detection, or physics<br />

calculation. It is important to note that if you use a Timeline object (class), you will be defining variables in system<br />

memory for frame rate and at least one KeyFrame object reference (these are part of the Timeline class definition) as<br />

well as properties (variables) inherited from the Animation superclass, such as status, duration, delay, cycleCount,<br />

cycleDuration, autoReverse, currentRate, currentTime, and an onFinished (ActionEvent) ObjectProperty.<br />

If you are familiar with creating animations, you will see that Timeline, along with at least one KeyFrame<br />

object and potentially a large number of KeyValue objects stored inside each KeyFrame object, is clearly designed<br />

for (optimized toward) creating timeline-based animation. Although this is a very powerful feature, it also means<br />

that using a Timeline and KeyFrame object for game loop processing will create close to a dozen areas of memory<br />

allocation that you may not even use in your game or that may not be designed (coded) optimally for your game<br />

design implementation.<br />

Fortunately, there is another javafx.animation package timing-related class that carries none of this prebuilt class<br />

overhead, and so I term this the lowest-level approach, in which you have to build all your game processing logic<br />

yourself, inside one simple .handle() function, which accesses the <strong>Java</strong>FX pulse engine on every pass it makes.<br />

The low-level solution involves using the AnimationTimer class, so named because <strong>Java</strong> (Swing) already has a<br />

Timer class (javax.swing.Timer), as does <strong>Java</strong>’s utility class (java.util.Timer), which you could also use if you were an<br />

advanced enough programmer to deal with all the thread synchronization issues (and coding).<br />

Because this is a beginner-level book, you will stick with looping your game using the <strong>Java</strong> 8 game engine<br />

(<strong>Java</strong>FX 8). <strong>Java</strong>FX has its own Timer class, in the javafx.animation package, called AnimationTimer so as not to<br />

cause confusion with Swing GUI Toolkit’s Timer class (which is still supported, for legacy code reasons). Many<br />

new developers are confused by the “Animation” part of this class name; do not assume that this Timer class is for<br />

Animation; it is, at its core, for timing purposes. This class is the lowest-level class in the javafx.animation package,<br />

in terms of accessing a <strong>Java</strong>FX pulse timing system, and essentially serves just to access the pulse timing system.<br />

Absolutely everything else is stripped away.<br />

The AnimationTimer class is therefore the class that will provide you with the least system overhead (memory<br />

used) to implement. At the full 60FPS speed, it will have the highest performance, assuming that all the code inside<br />

the .handle() method is well optimized. This is the class to use for a fast, high-dynamics game, such as an arcade game<br />

or a shooter game. For this reason, this is the class that you are going to use for your game, as you can continue to<br />

build a game engine framework and add features without running out of power.<br />

You will use the lowest-level approach throughout this book just in case you are pushing your <strong>Java</strong> 8 game<br />

development to the very limit and are creating a highly dynamic, action-filled game. The <strong>Java</strong>FX AnimationTimer<br />

superclass is perfect for this type of game application, as it processes its .handle() method on every single <strong>Java</strong>FX pulse<br />

event. A pulse event is currently throttled at 60FPS, the standard frame rate (also called a refresh rate) for professional<br />

action games. You will subclass your GamePlayLoop.java class from the AnimationTimer superclass.<br />

Interestingly, most modern iTV LCD, OLED, and LED display screen products also update at this exact refresh<br />

rate (60Hz), although newer displays will update at twice this rate (120Hz). Displays with a 240Hz refresh rate are<br />

also coming out, but because these 120Hz and 240Hz refresh rate displays use an even multiple (2× or 4×) of 60Hz,<br />

60FPS is a logical frame rate for developing games for today’s consumer electronics devices. Next, let’s implement the<br />

GamePlayLoop.java class in your game, which will subclass AnimationTimer to access pulses.<br />

Creating a New <strong>Java</strong> Class: GamePlayLoop.java<br />

Let’s use the AnimationTimer superclass from the javafx.animation package to create a custom GamePlayLoop class<br />

(and, eventually, object) and the required .handle() method to process your game play calculations. As Figure 7-2<br />

demonstrates, this is done in NetBeans 8.0 by right-clicking the invincibagel package folder in your Projects<br />

hierarchy pane. This will show NetBeans where you want the new <strong>Java</strong> class to be placed after it is created.<br />

www.it-ebooks.info<br />

147

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!