28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4 ■ An Introduction to <strong>Java</strong>FX 8: Exploring the Capabilities of the <strong>Java</strong> 8 Multimedia Engine<br />

Table 4-2. (continued)<br />

Package Name Functions Description of Contents<br />

javafx.print Printing Printing classes<br />

javafx.scene Scene control Classes related to scene creation, organization, control, and realization<br />

javafx.stage Stage creation Stage creation classes<br />

javafx.util <strong>Java</strong>FX utility <strong>Java</strong>FX utility classes<br />

netscape.javascript <strong>Java</strong>Script Allows <strong>Java</strong> code to invoke <strong>Java</strong>Script methods and examine<br />

<strong>Java</strong>Script properties<br />

I have discussed some of these already, such as the javafx.application package (see Chapters 2 and 3) and the<br />

javafx.scene package (see the section “<strong>Java</strong>FX Scene Package: Sixteen Powerful <strong>Java</strong> 8 Classes”). There are a few other<br />

<strong>Java</strong>FX packages that you should take a closer look at here, as they (along with the javafx.scene package) contain<br />

classes that you will want to use in your <strong>Java</strong> 8 game development (still others, such as the javafx.print, javafx.fxml,<br />

javafx.beans, and javafx.embed packages are not likely to be used in your <strong>Java</strong> game design and development work<br />

process); these are javafx.animation, javafx.stage, javafx.geometry, javafx.concurrent, and javafx.event. Let’s take<br />

an in-depth look at what these packages provide for your game development objectives next.<br />

<strong>Java</strong>FX Animation for <strong>Games</strong>: Using javafx.animation Classes<br />

The javafx.animation package contains the Animation superclass, which has the Timeline and Transition subclasses<br />

as well as the AnimationTimer, Interpolator, KeyFrame, and KeyValue classes. Animation is an important design<br />

element in <strong>Java</strong> 8 games, and these animation classes are already coded for us, thanks to <strong>Java</strong>FX, so all you have to do<br />

to add animation to your games is use them properly!<br />

The <strong>Java</strong>FX Animation Class: The Foundation for Animation Objects<br />

The Animation class (or object, actually) provides the core functionality of animation in <strong>Java</strong>FX. The Animation<br />

class contains two (overloaded) Animation() constructor methods; these are Animation() and Animation(double<br />

targetFramerate), and they will create in memory the Animation object, which will control an animation and its<br />

playback characteristics and life cycle.<br />

The Animation class contains the .play() method, the .playFrom(cuePoint) or .playFrom(Duration time)<br />

method, and a .playFromStart() method. These methods are used to start playback for the Animation object. There<br />

are also the .pause() method, which can pause the animation playback, and a .stop() method, to stop animation<br />

playback. The .jumpTo(Duration time) and .jumpTo(cuePoint) methods are used to jump to predefined positions<br />

in an animation.<br />

You can set the animation playback speed (also called the frame rate or frames per second [FPS]) by using the<br />

rate property. The cycleCount property (variable) allows you to specify how many times an animation will loop,<br />

and a delay property lets you specify a delay time before the animation starts. If your animation is looping, this delay<br />

property will specify the delay time between loops, which can help you create some realistic effects.<br />

You can specify a seamless animation loop by setting the cycleCount attribute or property (variable) to<br />

INDEFINITE and then using the autoReverse property (set to false), or you can use pong (back and forth) animation<br />

looping by specifying the true value for the autoReverse property. You can also set the cycleCount to a numeric value<br />

(use 1 if you want the animation to play only one time) if you do not want the animation to loop indefinitely.<br />

The .setRate() method sets the animation playback rate property, the .setDelay() method sets the delay<br />

property, and the .setCycleCount() and .setCycleDuration() methods control the cycling characteristics. There<br />

are also similar .get() methods to “get” the currently set values for these Animation object variables (properties,<br />

attributes, parameters, or characteristics; however you prefer to look at these data fields is fine).<br />

www.it-ebooks.info<br />

85

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

Saved successfully!

Ooh no, something went wrong!