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

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

Figure 13-12. Add if() statement logic to exclude processing of nested if-else hierarchy if down or up keys being pressed<br />

The reason that I’m doing this is because when either the up or down key is being pressed, the jump (or fly) or<br />

preparing to land sprite cel image is showing. For this reason, optimization dictates that I need to turn off all of the<br />

“alternating between sprite cel 1 and sprite cel 2” processing code. I want to do this so that this constant processing<br />

is not going on in memory and in the thread (CPU) when the imageStates(1) and imageStates(2) are not even being<br />

used (displayed) inside of the spriteFrame ImageView “container.”<br />

To achieve this optimization objective, I added a new level of evaluation to the outside of the second nested if()<br />

loop. This is the level that contains all of the sprite cel changes (1 to 2 and back) using the animator, framecounter, and<br />

runningspeed variables and related processing. This new more complex evaluation statement guarantees that all of<br />

this run-cycle processing logic will not be executed if this the new condition is being met.<br />

What this new condition says specifically, is that if the animator variable is false AND the up AND down keys<br />

are both not being used (are false) then to process the rest of the logic, which switches between the two sprite image<br />

states (as well as waiting a certain number of pulse events before each switch). What this means is that if either of the<br />

up or down keys are being pressed (thus showing the fly or land sprite cel image state), none of the programming logic<br />

past that point will be processed at all, saving CPU processing cycles for the many other things that we are going to be<br />

adding into the game play that will need to use this “saved” processing overhead for other game play-related logic.<br />

www.it-ebooks.info<br />

287

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

Saved successfully!

Ooh no, something went wrong!