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

The InvinciBagel Run State: If KeyPressed Set imageState(1 & 2)<br />

As you have seen in Chapter 8, I am going to try and achieve an animated run state for the character using only two<br />

sprite cels, imageState(1) and imageState(2). This is about as optimized as you can get, both from an Image assets<br />

standpoint, as well as from a coding standpoint, as you are about to see. This is especially true given that you can’t<br />

animate anything such as a run cycle using a single image state. That said, we are going to create a lot of very realistic<br />

animation during this chapter by using single cel, well-designed, sprite states, in combination with the sprite motion<br />

code that we put into place in Chapter 12. The if(invinciBagel.isRight()) and if(invinciBagel.isLeft())<br />

statement constructs will initially be quite easy and straightforward, but these will grow far more complex as we add<br />

refining features during the course of this chapter. We will put the foundation into place for these first, then add the<br />

up and down conditional if() statements, and then later, we will refine the right and left KeyEvent handling. Inside of<br />

the if() constructs for the left and right arrow key (and A and D key movement), we will use the same chained method<br />

call that we used in the first (impatiently waiting state) if() construct, only here we will call the imageStates(1) or<br />

imageStates(2) sprite cels, from the List object, instead of using the imageStates(0) sprite cel. The <strong>Java</strong> code to<br />

change the sprite Image states to state 1 or 2, if the right or left key is pressed (true), should look like the following:<br />

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

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

}<br />

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

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

}<br />

As you can see in Figure 13-3, the <strong>Java</strong> code is error-free, and we are ready to use the Run > Project work<br />

process test this preliminary run mode. If you quickly press the left and right arrow keys in succession, you’ll see the<br />

InvinciBagel running!<br />

Figure 13-3. Add conditional if() statements that check for left/right movement, and sets the run sprite image states<br />

276<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!