28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 13 ■ Animating Your Action Figure States: Setting the Image States Based on KeyEvent Processing<br />

note that we don’t absolutely have to use the <strong>Java</strong> this keyword; I am using it for clarity of meaning, and so you could<br />

also simply utilize the method call without using dot notation, like this: setIsFlipH(false); if you like. The <strong>Java</strong> code<br />

for doing this simple bookkeeping addition can be seen highlighted in Figure 13-18, and looks like this:<br />

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

spriteFrame.setScaleX(1);<br />

this.setIsFlipH(false);<br />

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

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

this.setIsFlipH(true);<br />

}<br />

// nested conditional if() processing ommitted for brevity<br />

// nested conditional if() processing ommitted for brevity<br />

Figure 13-18. Be sure to set the isFlipH property for the Bagel object using .setIsFlipH() called off of the this (object)<br />

Next, let’s test all of the new sprite animation code to see if it does what we think that it logically should do! The<br />

code is complex, at least for the primary left and right arrow key character movement, but is quite well organized, and<br />

very logical, and I don’t see any problems with it from a logic standpoint, but testing it thoroughly using NetBeans is<br />

the only way to find out! Let’s do that next. To review the sprite image states, refer back to Chapter 8 (Figure 8-2).<br />

Testing the InvinciBagel Sprite Animation States: Run ➤ Project<br />

Now it is time to use the NetBeans Run ➤ Project work process and test the .setImageState() method, which now gets<br />

called after the .setXYLocation() and .setBoundaries() methods, but before the .moveInvinciBagel() method. So the<br />

logical progression as it sits now is check keypressed, set X and Y location based on that, check to make sure you have<br />

not gone past any boundaries, set the sprite animation (Image) state, and then position the sprite. As you can see in<br />

Figure 13-19, the InvinciBagel character now runs realistically across the screen when you use left or right arrow keys.<br />

www.it-ebooks.info<br />

295

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

Saved successfully!

Ooh no, something went wrong!