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

attack unpredictable. Remember that we are starting simple, and adding complexity as we develop this code. The <strong>Java</strong><br />

code for the basic initiateAttack() method body is shown error-free in Figure 17-33, and should look like the following:<br />

private void initiateAttack() {<br />

if(takeSides) {<br />

spriteFrame.setScaleX(1);<br />

this.setIsFlipH(false);<br />

spriteFrame.setTranslateX(500);<br />

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

takeSides = false;<br />

} else {<br />

spriteFrame.setScaleX(-1);<br />

this.setIsFlipH(true);<br />

spriteFrame.setTranslateX(100);<br />

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

takeSides = true;<br />

}<br />

}<br />

Figure 17-33. Add the logic inside the if-else structure that flips the sprite and positions it on either side of the screen<br />

422<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!