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

Figure 13-7. Duplicate the nested if-else statement in .isLeft() structure, so InvinciBagel character runs both directions<br />

The next thing that we’ll need to is to add some <strong>Java</strong> code to control the running speed of your InvinciBagel<br />

sprite. We will do this using two Integer variables: one to serve as a framecounter and the other to hold the running<br />

speed value, which we can change later on based on our vX (velocity along the X axis) variable to get a realistic match<br />

between the speed the run cycle is animating at and the speed the sprite is being moved across the screen.<br />

Controlling Run Cycle Speed: Setting Up Your Animation Throttle Program Logic<br />

In order to be able to “throttle” our run cycle sprite animation to achieve different speeds, we need to introduce a<br />

“counter” variable called framecounter, which will count up to a certain number of frames before we change a false<br />

(sprite cel 1) animator value to true (sprite cel 2). We will also use a runningspeed variable, so that our animation<br />

speed is not hard-coded, and exists in a variable that we can change later on. This allows us to have fine-tuned control<br />

over the speed (realism) of this run-cycle animation. Declare these two Integer (int) variables at the top of the Bagel.<br />

java class and initialize the framecounter variable to zero and set the runningspeed variable to a value of 6. Since both<br />

the false (!animator) and true (animator) second-level if() structures will use this “count up to 6” variable, the math for<br />

what we are doing would equate to 6+6=12, divided into the 60FPS pulse timing loop, means that we are slowing down<br />

the unthrottled animation by 500% (five times, because 60/12=5). The variable declaration statements at the top of the<br />

Bagel class should look like the following <strong>Java</strong> code, and are also shown in the middle of Figure 13-8:<br />

int framecounter = 0;<br />

int runningspeed = 6;<br />

281

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

Saved successfully!

Ooh no, something went wrong!