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 17 ■ Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine<br />

Figure 17-40. Modify the Enemy() constructor method in the Enemy class to add an InvinciBagel object named iBagel<br />

Now that the Enemy class (object) can see into the InvinciBagel class (object), we’re ready to add projectiles.<br />

Creating a Projectile Infrastructure: Adding Projectile Variables<br />

To add projectile support to the Enemy class we will need to declare four new variables at the top of the class. These<br />

will include randomLocation, a new variable that we’ll use for both the Enemy character and the projectile he shoots;<br />

randomOffset, a new variable that will hold the vertical (Y) offset that allows us to fine-tune position the projectile so<br />

that it comes out of the bazooka barrel; and the bulletRange and bulletOffset allowing us to do X positioning. We will<br />

set the randomLocation variable equal to the randomNum.nextInt(attackBoundary) logic, which used to be inside<br />

of the .setTranslateY() method call, and add 5 to this variable, to create the randomOffset variable data value.<br />

The new <strong>Java</strong> structure for these methods is error-free, as shown in Figure 17-41, and should look like the following<br />

<strong>Java</strong> code:<br />

int spriteMoveR, spriteMoveL, destination;<br />

int randomLocation, randomOffset, bulletRange, bulletOffset;<br />

public void update() {<br />

if(callAttack) {<br />

if(attackCounter >= attackFrequency) {<br />

attackCounter=0;<br />

spriteMoveR = 700;<br />

spriteMoveL = -70;<br />

www.it-ebooks.info<br />

431

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

Saved successfully!

Ooh no, something went wrong!