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-8. Add int variables at top of the class for framecounter and runningspeed and set to zero in no movement if<br />

As you can see in Figure 13-8, I’ve clicked on the framecounter variable, so it is highlighted, and you can see its<br />

use in an initialization statement that we’ll need to put into the “no arrow keys pressed” conditional if() structure, just<br />

as we did with the animator variable. The code for this if() structure is shown in Figure 13-8, and looks like this:<br />

if( !invinciBagel.isRight() &&<br />

!invinciBagel.isLeft() &&<br />

!invinciBagel.isDown() &&<br />

!invinciBagel.isUp() ) {<br />

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

animator=false;<br />

framecounter=0;<br />

}<br />

Just like we want the animator Boolean variable to be reset to the false value any time that all of the arrow keys<br />

are not in use, so to do we want the framecounter integer variable to be reset to a zero value, any time that the arrow<br />

keys are not in use (that is, are in a released state all at the same time). As you can see, we not only are setting our<br />

sprite’s waiting image state in this conditional statement, we are also using it to reset our other variables as well.<br />

Now we are ready to make our .isRight() and .isLeft() conditional if structures even more complicated, as we will<br />

be nesting our <strong>Java</strong> logic three-nested conditional if structures deep, to allow us to incorporate the framecounter and<br />

runningspeed integer variables into our conditional if() structure. This will make our animation code “wait” for six<br />

pulse event cycles before it changes the animator false value to true, and then wait for another six pulse event cycles<br />

before changing it back to false.<br />

This is fairly complicated <strong>Java</strong> code, at least for a beginner <strong>Java</strong> 8 title, but game programming is inherently a<br />

complex undertaking, so let’s go ahead and learn how to code this throttle mechanism for our run-cycle animation.<br />

I want to teach you about advanced topics during this book, and this one (implementing a speed throttle) is one<br />

that we simply can’t avoid, as this run speed using a simple Boolean alternating image state logic structure is not<br />

feasible to use in our game, given the incredible speed of the <strong>Java</strong>FX pulse event timing engine and its “console game”<br />

60FPS frame rate, which is making our InvinciBagel sprite run cycle look not only unrealistic, but painful to look at!<br />

282<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!