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

You may be wondering why I have not added this same extended condition to the else if(animator) portion of<br />

this programming structure. The reason is that this portion of the loop will never be executed unless the first part,<br />

which we are excluding with this new statement, is processed. This is because inside of the first part of this loop, we<br />

set animator=true; and this will never happen (now that we have added the extended condition) if the up or down<br />

key is being pressed.<br />

What is really cool about this is that the if(invinciBagel.isRight()) and if(invinciBagel.isLeft())<br />

conditional if() structures can now be used to mirror all sprite cel state Image assets, when your player uses the left or<br />

right key to set the direction that the character is traveling in, and only when the left and right keys (only) are used to<br />

make the character run will the run-cycle part of this <strong>Java</strong> code processing take place.<br />

Make sure that you implement this same exact extended condition in your if(invinciBagel.isLeft()) part of<br />

the conditional if() structure, as is shown below, and which can be seen as well in Figure 13-13.<br />

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

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

if( !animator && ( !invinciBagel.isDown() && !invinciBagel.isUp() ) ) {<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 />

288<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!