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

Mirroring Sprites: Quadrupling Your Image Assets from 9 to 36<br />

Now let’s go back into the existing .isLeft() and .isRight() conditional evaluation statements, which are going to become<br />

quite “robust” (complicated) over the course of this chapter, and let’s add our spite mirroring capability. The <strong>Java</strong>FX<br />

API has its mirroring capability “hidden” in the ImageView class’s .setScaleX() method call. Although we are not going<br />

to scale our Image assets, as doing so causes artifacts in our pristine PNG32 image assets, there is a little known trick<br />

that you can pass a -1 (negative 100% scaling factor) value into a .setScaleX() method, to flip or mirror the Image asset<br />

around the Y axis (or into the .setScaleY() method, to flip or mirror around the X axis). Clearly we will need to also<br />

“undo” this in the other conditional if() structure by passing a 1 (positive 100% scaling factor) into the same method<br />

call, which does not make a whole lot of sense (normally) as our Image scale is already 100% (not scaled) but in light<br />

that the -1 scale-flip factor might have been set previously, this is how we make sure that mirroring is disabled and<br />

we are again using our original sprite Image assets for that particular state. Your newly upgraded <strong>Java</strong> statements<br />

implementing sprite mirroring should now look like the following code, which is also shown highlighted in Figure 13-5:<br />

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

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

spriteFrame.setScaleX(1);<br />

}<br />

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

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

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

}<br />

Figure 13-5. Add a .setScaleX() method call to the .isRight() and .isLeft() evaluations to flip the sprite around the Y axis<br />

As you can see in Figure 13-5, your <strong>Java</strong> code is still error-free. It is interesting to note that we are calling the sprite<br />

mirroring method on the spriteFrame ImageView object, and not on the Image assets inside of this ImageView. This<br />

is of tantamount importance, because it means that we can use this one single line of code inside of .isRight() and<br />

.isLeft() to flip whatever sprite state (image) is showing inside of an ImageView! That’s highly optimized programming!<br />

Now that our sprite mirroring code is in place, we need to take care of the issue of having the run cycle that is<br />

implemented by alternating imageStates(1) and imageStates(2) accomplished using our conditional if() processing.<br />

278<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!