Game Development Reference
In-Depth Information
tural reference to an object. This reference contains all of the contextual information
which would be needed to give the object that it is being passed to enough information
to be able to put everything into “digital perspective” (context) regarding the class that
is passing the this reference object over, in our case, this will be the InvinciBagel class
(object) passing contextual information about itself to the Bagel class (object). This
will include your object structure (variables, constants, methods, etc.) for the object as
well as state information relating to more complex things that relate to use of system
memory and thread usage. Think of passing one object's context information to another
object using the Java this keyword as wiring them together, so to speak, so that your
receiving object can peer into the sending object by using the this object reference.
Now that the this reference (contextual object) for the InvinciBagel class (object)
has been passed to the Bagel class (object) by using the Java this keyword inside of the
Bagel() constructor method, we have now create a far more industry standard link
between these two classes. We can now proceed to change our Bagel .update() method
so that it uses the new .is() method calls to get the four different Boolean variable val-
ues (states) from the InvinciBagel object reference that it now has. We will need this
data to be able to move our InvinciBagel character around on the screen.
Using Your New InvinciBagel .is() Methods: Updating
Your Bagel .update() Method
The next step in our elimination of using import static references and static variables
will be to rewrite the conditional if statements using the .isUp(), isDown(), isLeft() and
isRight() method calls. Since we're not using static variables to reach across classes
(objects) anymore, we'll need to replace these actual up , down , left, and right static
variables that are currently used inside the if() statements in the Bagel class .update()
method. These will no longer work, because they are now encapsulated in the Invin-
ciBagel class, and are private variables, so we will have to use .isUp(), isDown(),
isLeft(), and isRight() “getter” methods instead, to politely knock on the InvinciBagel's
door, and ask for these values!
We will call our four .is() methods “off of” the InvinciBagel reference object (using
dot notation), which we have declared and named invinciBagel at the top of the Ba-
gel.java class. This variable (object reference) contains the InvinciBagel class context,
which we sent from the InvinciBagel class into the Bagel class using the Java this
keyword. What this means is that if we say invinciBagel.isRight() in our code, our Ba-
gel class (object) now knows that to mean: go into the invinciBagel InvinciBagel object
using “this” reference object (just trying to be cute here), which will now show the Ba-
Search WWH ::




Custom Search