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

Figure 17-30. Create a conditional if using attackCounter inside the .update( ) method that calls initiateAttack( ) method<br />

Most of this “auto-attack” code we will be writing during this admittedly advanced chapter will leverage this<br />

.update() method, which will be called from the .handle() method that runs our game from the GamePlayLoop class.<br />

The reason I installed this .update() method that you need to override in every Actor subclass, is in case you wanted to<br />

animate things in the game. If an Actor is static, the .update() method simply exists as an empty or unused method.<br />

Attacking on Both Sides of the Screen: .initiateAttack() Method<br />

The way to make your attack come from both sides of the screen is to have a boolean variable that can be set to right<br />

(true) or left (false), which we will call takeSides. Declare this variable at the top of the Enemy class, and then create<br />

an empty private void initiateAttack(){} method structure underneath your .update() method. Inside of this<br />

.initiateAttack() method, create an empty if-else structure if(takeSides){}else{} to hold your attack programming<br />

logic, so private void initiateAttack(){if(takeSides){}else{}} if you are into writing super compact <strong>Java</strong><br />

code (this is valid <strong>Java</strong> code, but does absolutely nothing thus far). If you follow the industry standard <strong>Java</strong> 8 code<br />

formatting practices, the <strong>Java</strong> method body that you would use to implement this empty infrastructure can be seen in<br />

Figure 17-31, and should look like the following:<br />

boolean takeSides = false;<br />

private void initiateAttack() {<br />

if(takeSides) {<br />

// Empty Statement<br />

} else { // Empty Statement }<br />

}<br />

www.it-ebooks.info<br />

419

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

Saved successfully!

Ooh no, something went wrong!