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

The <strong>Java</strong> code for the .isRight() and .isLeft() conditional if() structures is going to become significantly more<br />

robust now, as we will have to nest another if-else conditional statement inside of the one that determines if the right<br />

arrow key is pressed. If right is true, it will set the ScaleX property to 1 (not mirrored), and then adds a conditional if()<br />

statement that looks to see if the value of the animator Boolean variable is false. If animator is false we use the handy<br />

method chain to get imageStates(1), and set this Image asset as the cel that the spriteFrame ImageView will use. After<br />

that, we need to set the animator variable to a true value, so that later on the imageStates(2) full run sprite image can<br />

be set. If animator is true, the else-if portion of the structure then looks to confirm animator is true, and if it is, again<br />

uses the spriteFrame.setImage(imageStates.get(2)); method chain to get imageStates(2) and sets animator to<br />

false. The new code for the statement, which is also shown in Figure 13-6, should look like the following:<br />

if(invinciBagel.isRight()) {<br />

spriteFrame.setScaleX(1);<br />

if(!animator) {<br />

spriteFrame.setImage(imageStates.get(1));<br />

animator=true;<br />

} else if(animator) {<br />

spriteFrame.setImage(imageStates.get(2));<br />

animator=false;<br />

}<br />

}<br />

It is important to note that you could remove the else if(animator) in this situation, and just use an else<br />

without the if(animator) part. However, we’re going to be making the right (and left) KeyPressed construct even more<br />

complex, by nesting even more code even deeper in the if-else-if-else structure, so I am going to leave it this way both<br />

for readability as well as for future code development purposes. As you can see in Figure 13-6, the code is error-free.<br />

You can now implement the same exact code structure into your .isLeft() conditional if() structure. Since the<br />

player will be using either the left or the right key (but not both together, at least not until we start adding those cool<br />

hidden “Easter egg” features during later stages of the game development) we can use the same animator variable in<br />

both the .isRight() and .isLeft() conditional if() constructs, allowing us to do a bit of memory-use optimization here.<br />

As you can see, the only difference is that the ScaleX property is set to mirror the sprite image (using a -1 value), and<br />

the <strong>Java</strong> code for the if(invinciBagel.isLeft()) conditional if() structure should therefore look like the following:<br />

if(invinciBagel.isLeft()) {<br />

spriteFrame.setScaleX(-1);<br />

if(!animator) {<br />

spriteFrame.setImage(imageStates.get(1));<br />

animator=true;<br />

} else if(animator) {<br />

spriteFrame.setImage(imageStates.get(2));<br />

animator=false;<br />

}<br />

}<br />

As you can see in Figure 13-7, this <strong>Java</strong> code is error-free, and you are ready to use your Run > Project work<br />

process, and test the InvinciBagel run cycle so you can see just how fast your superhero can run (or how fast the pulse<br />

engine in <strong>Java</strong>FX can be, using the AnimationTimer superclass for your GamePlayLoop class). When you test your <strong>Java</strong><br />

code, you will see that your superhero character is running much faster than humanly possible (and much faster than<br />

a bagel can run); in fact, the sprite animation cels are alternating so fast, that it looks like one blurred run animation!<br />

280<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!