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

What we want to do is to add a bulletGravity (or cheeseGravity) factor to the randomOffset (Y location) on each<br />

frame, so we get a slight “tailing off” effect on the shot. This will simulate gravity pulling the Projectile down to Earth.<br />

We will put this inside of the if(bulletOffset >= bulletRange) counter loop so that gravity factor is only applied when<br />

the projectile is visible flying across the screen. The <strong>Java</strong> code that incorporates bulletGravity and cheeseGravity<br />

adjustments to the Projectile object’s trajectories can be seen in Figure 17-60, and should look like the following code:<br />

private void shootProjectile() {<br />

if(!bulletType && !takeSides) {<br />

invinciBagel.iBullet.spriteFrame.setTranslateY(randomOffset);<br />

invinciBagel.iBullet.spriteFrame.setScaleX(-0.5);<br />

invinciBagel.iBullet.spriteFrame.setScaleY(0.5);<br />

bulletRange = -50;<br />

if(bulletOffset >= bulletRange) {<br />

bulletOffset-=6;<br />

invinciBagel.iBullet.spriteFrame.setTranslateX(bulletOffset);<br />

randomOffset = randomOffset + bulletGravity;<br />

} else { shootBullet = false; }<br />

}<br />

if(!bulletType && takeSides) {<br />

invinciBagel.iBullet.spriteFrame.setTranslateY(randomOffset);<br />

invinciBagel.iBullet.spriteFrame.setScaleX(0.5);<br />

invinciBagel.iBullet.spriteFrame.setScaleY(0.5);<br />

bulletRange = 624;<br />

if(bulletOffset = bulletRange) {<br />

bulletOffset-=4;<br />

invinciBagel.iCheese.spriteFrame.setTranslateX(bulletOffset);<br />

randomOffset = randomOffset + cheeseGravity;<br />

} else { shootBullet = false; }<br />

}<br />

if(bulletType && takeSides) {<br />

invinciBagel.iCheese.spriteFrame.setTranslateY(randomOffset);<br />

invinciBagel.iCheese.spriteFrame.setScaleX(0.5);<br />

invinciBagel.iCheese.spriteFrame.setScaleY(0.5);<br />

bulletRange = 630;<br />

if(bulletOffset

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

Saved successfully!

Ooh no, something went wrong!