Game Development Reference
In-Depth Information
was scratch-coded specifically to generate random numbers using the Java Object mas-
ter class. The class hierarchy for the Random class looks like the following:
java.lang.Object
> java.util. Random
The Random class has two constructors, a Random() constructor, which we will be
using, and an overloaded Random(long seed) constructor, where you can specify the
seed value for the Random Number Generator that this class implements. Once you
have a Random object, you can call one of almost two dozen (22) methods that gener-
ate different types of random values. The method calls we'll be using during this
chapter are the .nextInt(int bound) and .nextBoolean() method calls. In case you are
wondering, there is also a .nextInt() method, but we need to generate a random number
inside of a specific range (zero to bottom of screen), and the .nextInt(int bound) allows
generation of a random number range from zero to the specified integer bound (bound-
ary) passed over inside of the method call.
We will be using the Random class for our Enemy attack strategy (and code) in the
Enemy.java class. Let's go ahead and declare and instantiate a Random object named
randomNum at the top of the Enemy.java class, as shown in Figure 17-28 , using the
following compound (declaration plus instantiation) Java 8 programming statement:
protected static final Random randomNum = new Random();
 
 
Search WWH ::




Custom Search