28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 17 ■ Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine<br />

Figure 17-35. Use the Run ➤ Project to test your code; left half shows left side attack, right half shows right side attack<br />

To keep our use of <strong>Java</strong>FX Scene Graph Node objects optimized, we will reuse Projectile objects once they hit the<br />

InvinciBagel. This “bullet recycling” will be done using <strong>Java</strong> programming logic in several methods that we’ll put into<br />

place as we continue to write more and more advanced game play program logic throughout this chapter.<br />

Now we are ready to make the Enemy move on and off-screen, to add the element of surprise. We will be coding<br />

this animation rather than using another Animation class, since we are trying to do everything using only one<br />

AnimationTimer class (object) as an optimization strategy, which is working out amazingly well thus far.<br />

Adding the Element of Surprise: Animating Your Enemy Attack<br />

To animate our Enemy onto the Stage we will need to define boolean variables that hold the “on-screen off-screen”<br />

state, which I will call onScreen, as well as one that will serve as a switch that I can flip once the Enemy is visible on<br />

the screen, telling him to mount his attack, which I will name callAttack. We will also need integer variables to hold<br />

the current Enemy sprite right and left side X location, named spriteMoveR and spriteMoveL, and a destination<br />

variable that holds where we want the Enemy to stop and fire his Projectile objects. The <strong>Java</strong> declaration statements<br />

can be seen highlighted near the top of Figure 17-36, and should look like the following <strong>Java</strong> code:<br />

boolean onScreen = false;<br />

boolean callAttack = false;<br />

int spriteMoveR, spriteMoveL, destination;<br />

424<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!