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 />

You can assign an action to be executed when the animation has completed playback, using the onFinished<br />

property loaded with an ActionEvent object. The action will be executed when the animation reaches the end of each<br />

loop, and, as you can imagine, some very powerful things can be triggered in a game with this particular capability.<br />

There are also read-only variables (properties), which you can “poll” at any time to find the status, currentTime,<br />

currentRate, cycleDuration, and totalDuration for each Animation object. For example, you can use the currentTime<br />

property to see the position of the playback head (frame pointer) at any point in time in the animation playback cycle.<br />

The <strong>Java</strong>FX TimeLine Class: An Animation Subclass for Property Timeline Management<br />

The <strong>Java</strong>FX Timeline class is a subclass of the <strong>Java</strong>FX Animation superclass, so its inheritance hierarchy starts with the<br />

<strong>Java</strong> 8 masterclass, java.lang.Object, progressing down to the Timeline class, as follows:<br />

> java.lang.Object<br />

> javafx.animation.Animation<br />

> javafx.animation.Timeline<br />

A Timeline object can be used to define a special kind of Animation object that is composed of <strong>Java</strong>FX values<br />

(properties) of the object type WritableValue. Because all <strong>Java</strong>FX properties are of that type, this class can be used to<br />

animate anything in <strong>Java</strong>FX, which means that its use is limited only by your imagination.<br />

As mentioned earlier, Timeline animations are defined using KeyFrame objects, created via the KeyFrame class,<br />

which both creates and manages these objects. A KeyFrame object is processed by a Timeline object, according to a<br />

time variable (accessed via KeyFrame.time) and properties to be animated, which are defined using the KeyFrame<br />

object’s values variable (accessed via KeyFrame.values).<br />

It is important to note that you need to set up your KeyFrame objects before you start running the Timeline<br />

object, as you cannot change a KeyFrame object within a running Timeline object. This is because it is put into system<br />

memory once it has been started. If you want to change a KeyFrame object in a running Timeline object in any way,<br />

first, stop the Timeline object; then, make the change to the KeyFrame; and, finally, start the Timeline object again.<br />

This will reload the Timeline object and its revised KeyFrame objects into memory with their new values.<br />

The Interpolator class interpolates these KeyFrame.values in the Timeline object, based on the timeline<br />

direction. Interpolation is a process of creating in-between (or tween) frames, based on the beginning and ending<br />

values. In case you are wondering how the direction is inferred, it is kept in the rate and the read-only currentRate<br />

property of the Animation superclass (which is a part of the extended Timeline subclass).<br />

Inverting the value of the rate property (i.e., making it negative) will reverse (toggle) the playback direction; the<br />

same principle holds when reading the currentRate property (a negative value signifies the reverse, or backward,<br />

direction). Finally, the KeyValue class (object) is used to hold the values inside the KeyFrame object.<br />

The <strong>Java</strong>FX Transition Class: An Animation Subclass for Transition<br />

Effects Application<br />

The <strong>Java</strong>FX Transition class is a subclass of the <strong>Java</strong>FX Animation superclass, so its inheritance hierarchy starts with<br />

the <strong>Java</strong> 8 masterclass, java.lang.Object, progressing down to the Transition class, as follows:<br />

> java.lang.Object<br />

> javafx.animation.Animation<br />

> javafx.animation.Transition<br />

The Transition class is a public abstract class, and, as such, it can only be used (subclassed or extended) to<br />

create transition subclasses. In fact, ten of these subclasses have already been created for you to use to create your<br />

own transition special effects; these are the SequentialTransition, FadeTransition, FillTransition, PathTransition,<br />

PauseTransition, RotateTransition, ScaleTransition, TranslateTransition, ParallelTransition, and<br />

StrokeTransition classes. As a subclass of Animation, the Transition class contains all the functionality of Animation.<br />

86<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!