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-56. Add an attackFrequency statement incorporating a .nextInt(bounds) method in if (onScreen && launchIt)<br />

As you can see when you use the Run ➤ Project work process, you no longer can calculate when any given<br />

Enemy attack will commence! Let’s make the auto-attack engine more intelligent, by telling it where the InvinciBagel<br />

character is located (Y coordinate) on the screen, so that the auto-attack engine can more effectively target him!<br />

Targeting the InvinciBagel: Adding Enemy Artificial Intelligence<br />

The next thing that we should do to make game play more challenging is tell the auto-attack engine where the iBagel<br />

is located on the screen, an artificial intelligence gathering mission, made easier because we control all of the <strong>Java</strong><br />

code! To do this, instead of using the randomLocation random Y screen height location value, we will create a variable<br />

that will hold the InvinciBagel Y screen height location value, giving the Enemy inside information regarding where<br />

the iBagel object is on the screen. This is done using the iY attribute of the iBagel Hero object, which we access using<br />

the .getiY() getter method, and then use in the .setTranslateY() method call. We are using integer (rather than double)<br />

as the iBagelLocation data type, so we’ll need to “cast” the double data value we get from the .getiY() method, so that it<br />

is compatible with the iBagelLocation variable. The <strong>Java</strong> code, shown in Figure 17-57, should look like the following:<br />

int iBagelLocation;<br />

iBagelLocation = (int) invinciBagel.iBagel.getiY();<br />

spriteFrame.setTranslateY(iBagelLocation);<br />

randomOffset = iBagelLocation + 5;<br />

www.it-ebooks.info<br />

449

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

Saved successfully!

Ooh no, something went wrong!