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

}<br />

randomLocation = randomNum.nextInt(attackBoundary);<br />

spriteFrame.setTranslateY(randomLocation);<br />

randomOffset = randomLocation + 5;<br />

callAttack = true;<br />

} else { attackCounter+=1; }<br />

} else { initiateAttack(); }<br />

Figure 17-41. Add randomLocation, randomOffset, bulletRange, and bulletOffset variables to control bullet placement<br />

Also notice, at the bottom of Figure 17-41, we’ve also added an if(shootBullet){shootProjectile();}<br />

conditional if structure to the bottom of the .update() method, as well as adding a boolean shootBullet = false;<br />

declaration at the top of the Enemy.java class. Before we code the .shootProjectile() method, let’s add the shootBullet<br />

flag settings to the .initiateAttack() method, as well as setting a bulletOffset X location for the bullet, in if(!onScreen).<br />

432<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!