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

Once your conditional if() timer logic has expired (completed its countdown), your Enemy sprite is randomly<br />

positioned along the Y axis, moved into its starting position using the spriteMoveR and spriteMoveL variables, and the<br />

attackCounter is reset to zero for the next time the callAttack variable is set to false. At the end of the “set-up initiate<br />

attack” sequence of <strong>Java</strong> statements, callAttack is set equal to true, as seen in Figure 17-36, using the following code:<br />

public void update() {<br />

if(!callAttack) {<br />

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

attackCounter = 0;<br />

spriteFrame.setTranslateY(randomNum.nextInt(attackBoundary));<br />

spriteMoveR = 700;<br />

spriteMoveL = -70;<br />

callAttack = true;<br />

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

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

}<br />

Next we’re going to rewrite our if(takeSides) logic structure to remove the Y axis random number positioning<br />

statements, reposition the takeSides boolean flag program logic, and add an if(!onScreen) nested structure, around a<br />

.setTranslateX() method call. This will allow us to animate the iBeagle Enemy Actor sprite on and off of the screen.<br />

Inside of the if(!takeSides) structure, you’ll keep the first two statements that set the sprite mirroring (facing<br />

direction), but remove the .setTranslateY() method call as that is now accomplished in the .update() method. Add the<br />

if(!onScreen) conditional structure, where you’ll initialize the destination location to 500 pixels, and then nest another<br />

counter if(spriteMoveR >= destination) structure, inside of which you’ll move the sprite by two pixels per pulse<br />

using spriteMoveR-=2; in conjunction with spriteFrame.setTranslateX(spriteMoveR); to actually move the sprite<br />

as the counter changes, thus leveraging the counter variable in the sprite animation (movement) logic.<br />

The else part of the if-else structure will (eventually) shoot the projectile using a .shootProjectile() method we<br />

will be coding soon, and since the sprite is now on the screen, we will set the onScreen boolean flag variable to a true<br />

value, which, for now, will trigger the second if(onScreen) conditional logic structure. This will remove the Enemy<br />

sprite from the screen using half the velocity (move one pixel per counter iteration) that it came onto the screen with.<br />

The logic for the second nested conditional if(onScreen) structure is quite similar to the first. You will set the<br />

destination to 700 pixels (putting the Enemy sprite back off-screen, and again out of view), and this time, you’ll<br />

iterate using +=1 instead of -=2, which will not only move the Enemy in the opposite direction, due to a plus instead<br />

of minus, but will also use half the initial velocity used to mount an attack, because you’re moving by one pixel rather<br />

than two.<br />

The real difference in the if(onScreen) conditional if-else structure is in the else portion of the programming<br />

logic, where we not only set the onScreen boolean flag variable back to false, but we’ll also set the takeSides boolean<br />

variable to true, so that the Enemy will use the other side of the screen for his next attack attempt.<br />

Finally, since the attack sequence is completed, we will also set the callAttack boolean flag variable to false. As<br />

you know, this will start up the attackCounter program logic in the .update() method, which will give your player a few<br />

seconds to recover from being attacked. The <strong>Java</strong> structure for the entire if(!takeSides) conditional structure, and its<br />

nested if(onScreen) conditional structures, is shown highlighted in Figure 17-37, and should look like the following:<br />

private void initiateAttack() {<br />

if(!takeSides) {<br />

spriteFrame.setScaleX(1);<br />

this.setIsFlipH(false);<br />

if(!onScreen) {<br />

destination = 500;<br />

if(spriteMoveR >= destination) {<br />

spriteMoveR-=2;<br />

spriteFrame.setTranslateX(spriteMoveR);<br />

426<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!