Game Development Reference
In-Depth Information
For this reason, I am going to spend the first few pages of this chapter getting rid of
those import static Java statements, which although they work just fine, as you have
seen, they are not what is termed “ best practice ” in Java programming. There is a
more complex and involved way to talk between classes, involving a Java “this”
keyword, so I am going to show you how to implement far more private variables (and
far less static variables), and then I will teach you how to use reference objects, repres-
ented by the Java this keyword, to send object data variables between classes.
This is a somewhat advanced topic for a beginner level book, but it will allow you
to write more professional and “industry standard” Java 8 code, so it will be worth the
extra effort. Sometimes, the right way to do things is more involved and detailed than
the basic (simple) way to get things coded. The assumption here is that you are going
to be producing a commercially viable game, so you will need a rock solid foundation
to build increasingly complex code on.
After we finish adding additional code refinements in our InvinciBagel.java class,
which will implement Java “encapsulation” using private variables wherever possible,
and the this keyword where needed to provide access to the InvinciBagel object to oth-
er related classes—in this case, the GamePlayLoop and Bagel classes for now—we
will begin to add complexity to the sprite movement code that is in our Bagel class .up-
date() method.
We will add code that will tell your InvinciBagel character where the ceiling and
floor of his Scene and Stage are located, and where the left and right sides of the screen
are, so that he does not fall off of his flat 2D world. We will also organize the methods
in the Bagel.java class, so that the .update() method only calls higher-level methods
that contain all of the Java programming logic in an exceptionally well organized fash-
ion.
InvinciBagel Privatization: Removing
Static Modifiers
The first thing that I want to do regarding the InvinciBagel.java class Java code, as well
as the GamePlayLoop.java class code and the Bagel.java class code that both reference
the InvinciBagel up, down, left, and right Boolean variables by using import static
statements, is to remove these import static statements from the top of both of these
“worker” classes, and instead pass an InvinciBagel class ( context ) object, using the
Java this keyword, in the Bagel() constructor method as well as in the
GamePlayLoop() constructor method. The first step in this process, which will span the
Search WWH ::




Custom Search