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-32. Use a randomNum Random object inside of the .setTranslateY() method and use a period to call selector<br />

The next thing that you will want to do is to declare an integer variable named attackBoundary that will be<br />

referenced inside of your .nextInt() method call, so that you can change your Y axis (bottom of screen) boundary later<br />

on if you wish, in one easy-to-edit location at the top of the Enemy.java class. The <strong>Java</strong> statement should look like this:<br />

int attackBoundary = 300;<br />

Now we’re ready to finish writing the <strong>Java</strong> code that flips the iBeagle Enemy Actor (sprite) so he is facing the<br />

correct direction, and then we can test the code. It is important to write complex <strong>Java</strong> code in logical, digestible steps.<br />

In this way, you can test your code as you write it, making sure that each component of the programming logic works,<br />

before adding additional complexity. You will see this work process during this chapter, as we develop a robust autoattack<br />

algorithm inside of the Enemy.java class. This auto-attack code can be used for any of the Enemy objects that<br />

you create in the future; thus the code that you’re writing during this chapter will cover a plethora of bad guy attacks!<br />

It is important to note here that since takeSides is boolean and can only have two values – true or false, we’ll only<br />

need to implement a simple conditional if-else structure. This is because if our if(takeSides) condition equates to<br />

being true, then we know that the false value (else condition) logic structure will handle the takeSides=false scenario.<br />

Inside of both of these if{} and else{} attack logic processing structures, we’ll flip the sprite image around the Y<br />

axis, remembering to set the isFlipH variable for future use, set the sprite X location, to one side of the screen or the<br />

other, set the sprite Y location to a random height value on the screen and then set the takeSides boolean variable to<br />

the opposite of its current true or false data value. In this way, the iBeagle Enemy Actor object will alternate between<br />

the left and right sides of the screen. Later, we’ll use the .nextBoolean() method, from the Random class, to make the<br />

www.it-ebooks.info<br />

421

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

Saved successfully!

Ooh no, something went wrong!