Game Development Reference
In-Depth Information
Now that our sprite mirroring code is in place, we need to take care of the issue of
having the run cycle that is implemented by alternating imageStates(1) and im-
ageStates(2) accomplished using our conditional if() processing.
Animating Your Run Cycle: Creating a Nested If-Else
Structure
The next step in our sprite animation for this chapter is to actually animate a run cycle
for our character, which we would normally do with JavaFX KeyFrame and Timeline
classes, but which we will do here in little more than a dozen lines of code. Since we
are already using the AnimationTimer class, this is the optimal approach, and can be
done using only a single Boolean variable. Since we have two cels for our run cycle,
we can use this Boolean variable and alternate its value between true and false. If this
Boolean value, which we will call animator , is false, we will show the cel in im-
ageStates(1), which is our starting to run position (foot touching the ground). If animat-
or is true, we will show the cel in imageStates(2), which is our full-out run position
(both feet in full motion). Create the Boolean animator variable at the top of the Ba-
gel.java class. NetBeans was giving me a “variable not initialized” warning, so I expli-
citly set it equal to the default Boolean value of false , as I want the run cycle to always
start with a foot pushing off of the ground. The variable declaration statement should
look like the following, and is shown at the very top of Figure 13-6 :
boolean animator = false ;
 
 
Search WWH ::




Custom Search