Game Development Reference
In-Depth Information
Making the Enemy Pause Before Firing: pauseCounter
Variable
To make the Enemy pause on screen, so that his shooting action looks more realistic,
and so that the InvinciBagel character has some time to try and tackle him (which we
are going to assign ten scoring points to a bit later on), let's add an integer
pauseCounter
variable and a boolean
launchIt
variable at the top of our Enemy.java
class, as is shown highlighted in
Figure 17-44
. Inside of the if(shootBullet) conditional
statement, after the shootProjectile() method call, place an
if(pauseCounter >= 60)
conditional structure, and inside of it, set
launchIt
equal to
true
, and reset the
pauseCounter
variable to
zero
. In the else part of the condition, increment the
pauseCounter by one using
pauseCounter++
and then all we have to do is to imple-
ment the launchIt boolean flag into our initiateAttack() method and we will have an
Enemy character who takes his time when he aims and shoots at the InvinciBagel char-
acter. Your Java code for this if(shootBullet) if-else structure can be seen in
Figure
if(
shootBullet
) {
shootProjectile();
if(
pauseCounter >= 60
) {
launchIt = true
;
pauseCounter = 0;
}
else
{
pauseCounter++
}
}