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

Figure 13-11. Change .isDown() sprite cel to imageStates(6) using the imageStates.get(6) method call to use correct cel<br />

Let’s make sure that the professionalism of our InvinciBagel sprite animation is improving with each section of<br />

this chapter, and use the Run ➤ Project work process and test all four arrow keys. Be sure and test the up and down<br />

keypresses with the left and right keypresses (left-up, left-down, right-up, and right-down) to really see what the <strong>Java</strong><br />

code that you have put into place so far is capable of. We have a long way to go, but very impressive results already!<br />

Optimizing Run-Cycle Processing: Turning Off Processing for Fly and Land States<br />

The next thing that I want to do is also advanced conceptually, but uses far less code. As an optimization nut, what is<br />

concerning me is that the animator, framecounter, and runningspeed variables and the programming logic that uses<br />

them might be taking up memory when the player is using the up and down arrow keys, so I want to put a statement<br />

in the top of the conditional if() structure that leaves the .setScaleX() sprite mirroring code intact but turns off the rest<br />

of the processing logic if the up and down keys are being used. The <strong>Java</strong> code for excluding the run-cycle logic should<br />

be based on the up and down arrow key variables both showing as false, indicating the player is using the left or right<br />

keys only. This exclusion logic is seen in Figure 13-12, and looks like the following <strong>Java</strong> code (shown in bold) addition:<br />

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

spriteFrame.setScaleX(1);<br />

if( !animator && ( !invinciBagel.isDown() && !invinciBagel.isUp() ) ) {<br />

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

if(framecounter >= runningspeed) {<br />

animator=true;<br />

286<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!