Game Development Reference
In-Depth Information
amount of memory overhead. The AnimationTimer class is the most simple class, with
no class variables, and only a .handle() method to implement, and yet it is also the
most powerful, because it lets you write all of your own code.
This approach allows us to write custom code to animate a character based on keys
pressed and movement, rather than triggering predefined Timeline objects based on
KeyFrames (and their KeyValues) along that Timeline. I am keeping it simple on the
game engine side, and putting all of the complexity into our custom game play code.
This will save us a lot of headaches later on, trying to “synchronize” keyframe-based
and timeline-based linear animations, which puts us into a linear timeline-based
paradigm, such as Flash uses. This 100% Java 8 coding approach is certainly more dif-
ficult, from a Java coding perspective, but gives us an order of magnitude more power,
to achieve a seamless integration of event processing, screen movement, character an-
imation, physics, and collision detection. Setting up a multitude of prebuilt JavaFX An-
imation subclasses would presumably allow the same results in the end, but the code
would be less elegant, and probably much more difficult to build future versions of the
game play on top of.
All of our character state animation will be created using a .setImageState() meth-
od that will be called from inside of the .update() method, so, we will continue to be or-
ganized in the movement and animation of our character.
InvinciBagel
Animation:
The
.setImageState() Method
In this chapter, we are going to create one (fairly complex) method called
.setImageState() , which will set our InvinciBagel character's animation or motion
state based upon which keys are being pressed at any given moment. Calling a
.setImageState() method right before the .moveInvinciBagel() method in the .update()
method will serve to combine one of the character's nine image cels (frames) with the
motion of the character. This will create the illusion of animation, and will achieve this
without using any animation timelines whatsoever. From a game optimization stand-
point, this means that the JavaFX engine that is running our GamePlayLoop can focus
its resources on just that single animation (pulse) engine. As you can see in Figure
13-1 , we need to add a .setImageState(); method call, inside of the .update()
method before the .moveInvinciBagel() method call and after the .setBoundaries()
method call. After you do this, you'll have to create an empty method to get rid of the
error highlight. The Java code looks like this:
 
Search WWH ::




Custom Search