Game Development Reference
In-Depth Information
what to expect. Currently, we are alternating sides, so we need to add a randomization
of the
takeSides
boolean flag at the strategic place within our code. Let's write this
code next.
Randomizing an Auto-Attack: Using .nextBoolean
with takeSides
Even though we set the takeSides boolean flag right before we exit our initiateAttack()
method after completing an attack, there is nothing that says we can't set it again be-
fore we call an attack by setting callAttack equal to true in the
if(attackCounter >= at-
tackFrequency)
structure, which you can see I have done in
Figure 17-54
, using the
following Java statement:
takeSides = randomNum.nextBoolean;
Figure 17-54
.
Set the boolean takeSides variable to use a .nextBoolean() method call off a randomNum Random ob-
ject
Now that you have set the takeSides boolean flag variable in this way inside the
Enemy .update() method, you can further optimize your code by removing the
takeSides = true;
and
takeSides = false;
statements, which are cur-