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

InvinciBagel Animation: The .setImageState( ) Method<br />

In this chapter, we are going to create one (fairly complex) method called .setImageState(), which will set our<br />

InvinciBagel character’s animation or motion state based upon which keys are being pressed at any given moment.<br />

Calling a .setImageState() method right before the .moveInvinciBagel() method in the .update() method will serve to<br />

combine one of the character’s nine image cels (frames) with the motion of the character. This will create the illusion<br />

of animation, and will achieve this without using any animation timelines whatsoever. From a game optimization<br />

standpoint, this means that the <strong>Java</strong>FX engine that is running our GamePlayLoop can focus its resources on just that<br />

single animation (pulse) engine. As you can see in Figure 13-1, we need to add a .setImageState(); method call,<br />

inside of the .update() method before the .moveInvinciBagel() method call and after the .setBoundaries() method call.<br />

After you do this, you’ll have to create an empty method to get rid of the error highlight. The <strong>Java</strong> code looks like this:<br />

private void setImageState() { The Method Body we'll develop in this chapter goes in here }<br />

Figure 13-1. Create the private void setImageState( ) method; place a setImageState( ) method call in .update( ) method<br />

As you can see in Figure 13-1, this empty code framework doesn’t generate any red error or yellow warning<br />

highlights in the code. We are currently very organized, accomplishing all our KeyEvent handling, boundary<br />

detection, sprite animation, and sprite movement, by using only four method calls inside of this Bagel class<br />

.update() method.<br />

The first thing that we want to check for is no movement: that is, no keys pressed, so that we can correctly<br />

implement the “waiting” InvinciBagel state we used in the previous chapter to develop a sprite movement algorithm.<br />

The InvinciBagel Wait State: If No Key Pressed Set imageState(0)<br />

The first conditional if( ) statement that we put into place will be the default or “no keys pressed” state, which is sprite<br />

zero that shows the InvinciBagel waiting impatiently to be moved and animated. What we want to look for inside of<br />

the if evaluation area inside of the parenthesis is a false value for each of the up, down, left, and right variables, all at<br />

the same moment of time. Up until now, we have been looking for a true value, using the .isUp(), .isDown(), .isLeft(),<br />

and .isRight() method calls off of the invinciBagel object reference. In this situation we want to look for a false value.<br />

274<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!