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 13 ■ Animating Your Action Figure States: Setting the Image States Based on KeyEvent Processing<br />

Summary<br />

In this thirteenth chapter, we implemented sprite animation across the InvinciBagel application using highly<br />

optimized code. We combined seven basic sprite shapes with the key movement that we developed in Chapter 11 and<br />

the boundary detection that we developed in Chapter 12 to create a fully animated InvinciBagel character that really<br />

comes alive, and does so based on the use of six basic game play control keys (up, down, left, right, W, and S).<br />

We learned how to use the <strong>Java</strong>FX ImageView class ScaleX property with its special -1 use case setting to flip or<br />

mirror the Image asset that is inside of the ImageView “Image container” around the Y axis. This allows us to create<br />

36 sprite image states using only 9 basic sprite states, which we have imported as Image assets, into a List<br />

ArrayList object. This is a form of optimization, as it allows us to use less than 84KB of image assets instead of 336KB<br />

of Image assets for our primary game superhero, the InvinciBagel character.<br />

Next, we learned how to implement a Boolean animator variable, which we used to animate between two<br />

different sprite cel run states, imageStates(1) and imageStates(2) in our List. The resulting run cycle was too rapid for<br />

professional use, so next, we then added a framecounter variable, to slow the movement down, and a runningspeed<br />

variable, allowing us to implement a fine-tuned control over the sprite’s running speed, which we will be able to take<br />

advantage of in later on in our game play logic.<br />

Next we optimized our code so that the variables and processing code used for the run cycle were not used if the<br />

run cycle was not showing, that is, if the InvinciBagel is flying or landing. We also made sure to set the isFlipH property<br />

in our Actor superclass (and therefore in our Bagel object) using the this.setIsFlipH() method call.<br />

Next we added four new game play control keys in our event handling code in the InvinciBagel.java class, and<br />

added four new private Boolean variables aKey, sKey, dKey, and wKey, and had NetBeans create Getter and Setter<br />

methods for them automatically. After we made that enhancement, we added the sprite evade action image to the S<br />

key and the sprite jump over action image to the W key to make our game a two-hand game and to get ready to make<br />

the game prepared to use professional game controller hardware.<br />

Finally, we tested this new sprite animation code with the rest of the event handling, Actor and Hero class,<br />

CastingDirector class, GamePlayLoop class, and Bagel class sprite movement and boundary code that we have written<br />

over the previous six chapters (Chapters 7 through 12). Our sprite movement and animation work together seamlessly<br />

to provide a professional result when navigating our InvinciBagel character around the game play Stage.<br />

In the next chapter, we are going take a look at how to add fixed game sprite (Prop Actor) objects into the game,<br />

so that we have something to work with when we get into collision detection, and so we can start to really use our<br />

recent CastingDirector class.<br />

298<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!