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-27. Use a .toBack() method call in the gameButton code, and .toFront() method call in the other Button code<br />

Using Random Number Generators: java.util.Random<br />

The java.util package contains programming utilities that you can use for your <strong>Java</strong> 8 games development, as you<br />

may have guessed from the package name. One of the most important <strong>Java</strong> utilities for game programmers is the<br />

Random class, and its Random() constructor method. This class can be used to create Random Number Generator<br />

objects, which generate random number (or boolean) values for use inside your game programming logic. We will be<br />

using this class to generate both int (integer, for random screen locations) and boolean (for “guess where the Enemy<br />

is coming from next” functions) random values. These will make sure the game player cannot anticipate the game<br />

by recognizing patterns during the game play. The <strong>Java</strong> 8 Random class was scratch-coded specifically to generate<br />

random numbers using the <strong>Java</strong> Object master class. The class hierarchy for the Random class looks like the following:<br />

java.lang.Object<br />

> java.util.Random<br />

416<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!