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-43. Add private void shootProjectile() method and code the if(!takeSides) and if(takeSides) if-else statements<br />

If you now use your Run ➤ Project work process you will see your iBeagle animate on screen, shoot a bullet, and<br />

retreat quickly back off-screen. The next level of realism that we need to add into our code is to have the iBeagle pause<br />

for a second while he aims and shoots the bullet, because currently it looks like he is hitting an invisible barrier and<br />

bouncing back off-screen. We will do this by adding counter logic into the if(shootBullet) conditional statement in the<br />

.update() method. Let’s do that next, so that we have a completely professional Enemy auto-attack sequence!<br />

Making the Enemy Pause Before Firing: pauseCounter Variable<br />

To make the Enemy pause on screen, so that his shooting action looks more realistic, and so that the InvinciBagel<br />

character has some time to try and tackle him (which we are going to assign ten scoring points to a bit later on),<br />

let’s add an integer pauseCounter variable and a boolean launchIt variable at the top of our Enemy.java class, as<br />

is shown highlighted in Figure 17-44. Inside of the if(shootBullet) conditional statement, after the shootProjectile()<br />

method call, place an if(pauseCounter >= 60) conditional structure, and inside of it, set launchIt equal to true,<br />

and reset the pauseCounter variable to zero. In the else part of the condition, increment the pauseCounter by one<br />

using pauseCounter++ and then all we have to do is to implement the launchIt boolean flag into our initiateAttack()<br />

method and we will have an Enemy character who takes his time when he aims and shoots at the InvinciBagel<br />

character. Your <strong>Java</strong> code for this if(shootBullet) if-else structure can be seen in Figure 17-44, and should look like the<br />

following code:<br />

if(shootBullet) {<br />

shootProjectile();<br />

if(pauseCounter >= 60) {<br />

launchIt = true;<br />

pauseCounter = 0;<br />

} else { pauseCounter++ }<br />

}<br />

www.it-ebooks.info<br />

435

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

Saved successfully!

Ooh no, something went wrong!