Game Development Reference
In-Depth Information
Summary
In this twelfth chapter, we privatized our primary InvinciBagel.java class as much as
possible, and removed all of the static modifier keywords that were not specifically re-
lated to constants (WIDTH and HEIGHT). First we removed the public static Boolean
variables and made them private to the InvinciBagel class, and then created getter and
setter methods to allow the Bagel class to use these variables using .is() method calls.
We also had to pass the InvinciBagel object reference to the Bagel object inside of the
front of the Bagel() constructor parameter list using the Java this keyword. We made
these same changes to the static Bagel iBagel object declaration, removing the static
modifier keyword and passing the InvinciBagel object context using the Java this
keyword, this time inside of the GamePlayLoop() constructor method call. To do this
we had to create our own custom GamePlayLoop() constructor method rather than us-
ing the one that is created by the compiler (JVM) if we do not specifically provide one.
After that we removed the other two static modifier keywords on the StackPane and
HBox objects, and made all of the rest of the variables private, at least for now, to
provide the greatest level of encapsulation for the InvinciBagel (primary) game class.
Next, we reorganized the code in the Bagel.java class relating to the .update() meth-
od. We created specific methods for polling the keypressed values and setting the iX
and iY properties for the object, which we called the .setXYLocation() method, as well
as creating the .moveInvinciBagel() method for calling the .setTranslate() methods.
Finally, we created a new .setBoundaries() method in the Bagel.java class, which is
called after the .setXYLocation() method but before the .moveInvinciBagel() method
that makes sure that our main character stays on the screen at all times.
In the next chapter, we are going take a look at advanced concepts regarding anim-
ating the game sprite as it is moved around your screen using the List<Image> Ar-
rayList<Image> object, so that we get more realistic sprite animation before we get in-
to advanced topics such as digital audio, collision detection, and physics simulation.
Search WWH ::




Custom Search