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

That said, this is about as complex as our <strong>Java</strong> 8 coding is going to get during this chapter, at least, so hang on<br />

tight, and enjoy the ride (or rather, the run) as we create a 16 lines of <strong>Java</strong> code, nested conditional if() structure, in the<br />

next section. It will be a lot of work, but the resulting run-cycle throttle control will be well worth the effort!<br />

Coding Your Run Cycle Throttle: Triple Nested If-Else Structures<br />

The modification we are going to make to our Boolean animator if() structure is to place an if(framecounter >=<br />

runningspeed){...} structure “around” the animator=true; statement, so that the animator does not become true<br />

until six pulse event loops have transpired. If the framecounter is equal to (or for some reason, greater than) six,<br />

animator becomes true, and the framecounter gets reset to zero and imageStates(2) is used. If framecounter is less<br />

than six, the else part of the statement increments the framecounter by one with a framecounter+=1; statement. We<br />

wrap framecounter code around both the if(animator) code in both parts of this structure, as shown in Figure 13-9:<br />

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

spriteFrame.setScaleX(1);<br />

if(!animator) {<br />

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

if(framecounter >= runningspeed) {<br />

animator=true;<br />

framecounter=0;<br />

} else { framecounter+=1; }<br />

} else if(animator) {<br />

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

if(framecounter >= runningspeed) {<br />

animator=false;<br />

framecounter=0;<br />

} else { framecounter+=1; }<br />

}<br />

}<br />

www.it-ebooks.info<br />

283

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

Saved successfully!

Ooh no, something went wrong!