Game Development Reference
In-Depth Information
that the false value (else condition) logic structure will handle the takeSides=false
scenario.
Inside of both of these if{} and else{} attack logic processing structures, we'll flip
the sprite image around the Y axis, remembering to set the
isFlipH
variable for future
use, set the sprite X location, to one side of the screen or the other, set the sprite Y loc-
ation to a
random height
value on the screen and then set the
takeSides
boolean vari-
able to the opposite of its current true or false data value. In this way, the iBeagle
Enemy Actor object will
alternate
between the left and right sides of the screen. Later,
we'll use the
.nextBoolean()
method, from the
Random
class, to make the attack un-
predictable. Remember that we are starting simple, and adding complexity as we devel-
op this code. The Java code for the basic initiateAttack() method body is shown error-
free in
Figure 17-33
,
and should look like the following:
private void
initiateAttack()
{
if(
takeSides
) {
spriteFrame.setScaleX(
1
);
this.setIsFlipH(
false
);
spriteFrame.setTranslateX(
500
);
spriteFrame.setTranslateY(
randomNum
.nextInt(
attackBoundary
));
takeSides =
false
;
}
else
{
spriteFrame.setScaleX(
-1
);
this.setIsFlipH(
true
);
spriteFrame.setTranslateX(
100
);
spriteFrame.setTranslateY(
randomNum
.nextInt(
attackBoundary
));
takeSides =
true
;
}
}