Game Development Reference
In-Depth Information
animator=false;
}
}
It is important to note that you could remove the else if(animator) in this
situation, and just use an else without the if(animator) part. However, we're going to
be making the right (and left) KeyPressed construct even more complex, by nesting
even more code even deeper in the if-else-if-else structure, so I am going to leave it this
way both for readability as well as for future code development purposes. As you can
see in Figure 13-6 , the code is error-free.
You can now implement the same exact code structure into your .isLeft() condi-
tional if() structure. Since the player will be using either the left or the right key (but
not both together, at least not until we start adding those cool hidden “Easter egg” fea-
tures during later stages of the game development) we can use the same animator vari-
able in both the .isRight() and .isLeft() conditional if() constructs, allowing us to do a
bit of memory-use optimization here. As you can see, the only difference is that the
ScaleX property is set to mirror the sprite image (using a -1 value), and the Java code
for the if(invinciBagel.isLeft()) conditional if() structure should there-
fore look like the following:
if(invinciBagel. isLeft() ) {
spriteFrame.setScaleX( -1 );
if( !animator ) {
spriteFrame. setImage (imageStates. get(1) );
animator=true;
}
else if( animator ) {
spriteFrame. setImage (imageStates. get(2) );
animator=false;
}
}
As you can see in Figure 13-7 , this Java code is error-free, and you are ready to use
your Run > Project work process, and test the InvinciBagel run cycle so you can see
just how fast your superhero can run (or how fast the pulse engine in JavaFX can be,
using the AnimationTimer superclass for your GamePlayLoop class). When you test
your Java code, you will see that your superhero character is running much faster than
humanly possible (and much faster than a bagel can run); in fact, the sprite animation
cels are alternating so fast, that it looks like one blurred run animation!
 
Search WWH ::




Custom Search