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

Since this is not how we want to make our game player make the InvinciBagel run (because the very nature of it<br />

stops his movement around the screen, and because it’s just plain lame), let’s quickly put into place your up and down<br />

key support, so that we can come back and work on the left and right keys so we can make the run cycle work!<br />

The InvinciBagel Fly State: If KeyPressed Set imageState(3 & 4)<br />

The if(invinciBagel.isDown()) and the if(invinciBagel.isUp()) conditional if() structures are identical to the<br />

left and right key structures, except that they call imageStates(3) and imageStates(4) List elements, to allow the<br />

InvinciBagel character to “come in for landing” (cel 3), and “take off flying” (cel 4). As we add more of the imageStates<br />

during this chapter, and combine this cel animation code with our motion and boundary code, you are going to have<br />

more and more fun testing this chapter’s coding results! If you want to take a programmer’s shortcut, copy and paste<br />

the .isRight() and .isLeft() constructs underneath themselves, and simply change the .get(1) and .get(2) to .get(3) and<br />

.get(4). As you can see in Figure 13-4, the code is currently very compact and well organized; structured; and logical;<br />

and in only half a dozen lines of <strong>Java</strong> code, we have now implemented more than half of our nine image states already!<br />

Of course, we still have to add refinement code, to implement sprite mirroring for direction changes and run-cycle<br />

timing refinement. The <strong>Java</strong> code for the .isUp() and .isDown() method structures should look like the following:<br />

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

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

}<br />

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

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

}<br />

Figure 13-4. Add conditional if() statements that check for up/down movement and sets jump/land sprite image state<br />

As you can see in Figure 13-4, our code is error-free, and we are ready to add several layers of complexity to the<br />

left and right arrow key event processing code, since these two keys are defining the direction (East and West) the<br />

InvinciBagel is traveling. For this reason, these two conditional if() statement structures, in particular, need to become<br />

more complex, because traveling East (to the right) will use original (isFlipH = false) sprites, and traveling West (to the<br />

left) will utilize a mirrored version (isFlipH = true) of each sprite, “flipping” the Image assets around the central Y axis.<br />

www.it-ebooks.info<br />

277

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

Saved successfully!

Ooh no, something went wrong!