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

To accomplish this, we need to use the <strong>Java</strong> Unary Exclamation Point ! operator. This reverses the Boolean<br />

value, so in our case, a false value from one of these method calls would be represented by a !invinciBagel.isUp()<br />

construct, for instance. To find out if more than one value is false at the same time, we’ll need to implement the <strong>Java</strong><br />

Conditional AND operator, which uses two consecutive ampersand characters, like this && so; in this case, we will<br />

be using three of these && Conditional AND operators, to tell the <strong>Java</strong> compiler that we want Right AND Left AND<br />

Down AND Up to all be false. All of this logic will go inside of the if() evaluation area (inside of the parenthesis). Inside<br />

of the curly braces, where the statements go if the if() evaluation area is met (if up, down, left and right are all false),<br />

we will set the spriteFrame ImageView object equal to the first Image object in the imageStates List object<br />

using the .setImage() method call. Inside of that method call, we will use the .get() method call on the imageStates<br />

List object, to get the first Image reference imageStates(0) from the List object. This is the “waiting”<br />

sprite cel for the InvinciBagel, which shows him waiting impatiently to be moved (animated). The <strong>Java</strong> code for the<br />

construct would look like the following <strong>Java</strong> programming structure (I have indented this for easier readability and<br />

learning purposes):<br />

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

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

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

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

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

As is seen in Figure 13-2, this first if() statement, representing “if none of the arrow keys are being pressed,” is<br />

error-free. If you use the Run ➤ Project work process, and test this code, you’ll get the same result as you did in the<br />

previous chapter! To see if this code works we must first make the InvinciBagel run, so that when we stop moving him<br />

using the arrow keys, we get this impatient “waiting” state, which actually makes it a lot more effective (funny), when<br />

this wait is in the context of all this animated movement we’re about to implement during the course of the chapter!<br />

Figure 13-2. Add a conditional if() statement that checks for no movement, and sets the wait sprite image state (zero)<br />

Next let’s start to implement some of the other character image cels to try and get our character animating!<br />

www.it-ebooks.info<br />

275

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

Saved successfully!

Ooh no, something went wrong!