Game Development Reference
In-Depth Information
Figure 17-35 . Use the Run
Project to test your code; left half shows left side attack, right half shows right side
attack
To keep our use of JavaFX Scene Graph Node objects optimized, we will reuse
Projectile objects once they hit the InvinciBagel. This “bullet recycling” will be done
using Java programming logic in several methods that we'll put into place as we con-
tinue to write more and more advanced game play program logic throughout this
chapter.
Now we are ready to make the Enemy move on and off-screen, to add the element
of surprise. We will be coding this animation rather than using another Animation
class, since we are trying to do everything using only one AnimationTimer class (ob-
ject) as an optimization strategy, which is working out amazingly well thus far.
Adding the Element of Surprise: Animating Your
Enemy Attack
To animate our Enemy onto the Stage we will need to define boolean variables that
hold the “on-screen off-screen” state, which I will call onScreen , as well as one that
will serve as a switch that I can flip once the Enemy is visible on the screen, telling him
to mount his attack, which I will name callAttack . We will also need integer variables
to hold the current Enemy sprite right and left side X location, named spriteMoveR
and spriteMoveL , and a destination variable that holds where we want the Enemy to
stop and fire his Projectile objects. The Java declaration statements can be seen high-
lighted near the top of Figure 17-36 , and should look like the following Java code:
boolean onScreen = false;
boolean callAttack = false;
int spriteMoveR , spriteMoveL , destination ;
 
Search WWH ::




Custom Search