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

Animating Your Action Figure States:<br />

Setting the Image States Based on<br />

KeyEvent Processing<br />

Now that we have also organized your <strong>Java</strong> code into logical methods in the Bagel.java class, as well as making sure<br />

that all of our <strong>Java</strong> code is standards compliant in Chapters 11 and 12, it is time to get into some more complicated<br />

code structures that will animate our InvinciBagel character on the screen as the user moves the character. For<br />

instance, if the character is traveling due East or due West (using only left or right keypresses, to travel in a straight<br />

line), he should be running (alternating between the imageStates(1) and imageStates(2) List elements). If<br />

the up key is also pressed, he should be leaping up in the direction of the left or right keypress, and if the down key is<br />

pressed, he should be preparing to land in the direction of the left or right keypress.<br />

We will also need to implement the Actor class’s isFlipH property or attribute, so that the character is facing the<br />

right way based upon the direction he is traveling. Instead of using another image for that, we will use the <strong>Java</strong>FX<br />

capability to “flip” or “mirror” any image around its central Y axis (isFlipH) or around its central X axis (isFlipV). Once<br />

a sprite animation state is combined with the movement code that you put into place in the previous chapter, you’ll be<br />

amazed at how realistic this character will become, and we are still using only nine sprite state images (under 84KB of<br />

total data footprint used for our new media assets so far).<br />

We will do all of this character animation during this chapter using only <strong>Java</strong> code, and using only the <strong>Java</strong>FX<br />

AnimationTimer (GamePlayLoop) superclass. This way, we are optimizing the use of the <strong>Java</strong>FX pulse engine for our<br />

game by accessing the pulse event timing engine using only the javafx.animation package’s class that uses the least<br />

amount of memory overhead. The AnimationTimer class is the most simple class, with no class variables, and only a<br />

.handle() method to implement, and yet it is also the most powerful, because it lets you write all of your own code.<br />

This approach allows us to write custom code to animate a character based on keys pressed and movement,<br />

rather than triggering predefined Timeline objects based on KeyFrames (and their KeyValues) along that Timeline.<br />

I am keeping it simple on the game engine side, and putting all of the complexity into our custom game play code.<br />

This will save us a lot of headaches later on, trying to “synchronize” keyframe-based and timeline-based linear<br />

animations, which puts us into a linear timeline-based paradigm, such as Flash uses. This 100% <strong>Java</strong> 8 coding<br />

approach is certainly more difficult, from a <strong>Java</strong> coding perspective, but gives us an order of magnitude more power,<br />

to achieve a seamless integration of event processing, screen movement, character animation, physics, and collision<br />

detection. Setting up a multitude of prebuilt <strong>Java</strong>FX Animation subclasses would presumably allow the same results<br />

in the end, but the code would be less elegant, and probably much more difficult to build future versions of the game<br />

play on top of.<br />

All of our character state animation will be created using a .setImageState() method that will be called from<br />

inside of the .update() method, so, we will continue to be organized in the movement and animation of our character.<br />

www.it-ebooks.info<br />

273

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

Saved successfully!

Ooh no, something went wrong!