Game Development Reference
In-Depth Information
some exposure to these concepts before we finish this first (beginner) round of our core
Java 8 games development cycle.
Add the Element of Surprise: Randomizing the Attack
Frequency
Now that we have made the point of entry on the screen as well as the side of the
screen that is used for the attack to be completely random, let's go into the fourth di-
mension (time) and make when the attack will occur also random. This is done by ran-
domizing the attackFrequency variable, which before this we had set to 4.167 seconds
(250/60FPS). We will set this random value in the else structure in the .initiateAttack()
method where we set your boolean flag settings and call the .load() methods that we
created to make sure the auto-attack engine always has Enemy and Projectile objects to
work with. We will put a Java statement that inserts a random value into the attackFre-
quency variable at the end of this else portion of the if(onScreen && launchIt) condi-
tional structure, so that a new random time is in place when the .update() method starts
using this variable for its attack delay counter programming logic. Since the .nex-
tInt(int bounds) method call structure gives us a random integer between zero and the
upper bounds, in order to get a range of attack delay between one second (60) and nine
seconds (60+480), we will need to add 60 to the value generated by the ran-
domNum.nextInt(480) part of the statement, and then set the attackFrequency variable
equal to that value. The Java code for this attackFrequency randomization statement
can be seen in Figure 17-56 and should look like the following:
attackFrequency = 60 + randomNum.nextInt( 480 );
 
Search WWH ::




Custom Search