28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12 ■ Setting Boundaries for Your Action Figure in 2D: Using the Node Class LocalToParent Attribute<br />

Figure 12-3. Place the cursor at the bottom of the class so that the four .set() and .is() methods are the last ones listed<br />

In this case, the this.up refers to the private up variable inside the InvinciBagel object (InvinciBagel class).<br />

As you can see, this is the new (more complex, or at least more involved to put into place, code-wise) way that we<br />

can now access the up variable without having to reach across classes using a static modifier keyword and an import<br />

static declaration at the top of the Bagel.java class, which as you will see a bit later, we no longer need to use.<br />

Now that we have made our InvinciBagel class a bit more encapsulated (more private, and less public) by<br />

declaring the Boolean variables to be private, and putting getter and setter methods in place for classes and objects<br />

external to InvinciBagel to request that data, we will need to modify the Bagel class constructor method to receive a<br />

copy of the InvinciBagel object so that the calling class has “digital context” as to what the InvinciBagel class (and thus<br />

object) has to offer. This is done using an additional parameter, the <strong>Java</strong> this keyword, in the Bagel() parameter list.<br />

Passing Context from InvinciBagel to Bagel: Using this Keyword<br />

The final piece in the puzzle regarding how to eliminate static import statements, and reach between classes (objects)<br />

in a legitimate fashion, is to pass the InvinciBagel class’s current configuration, held in a contextual object reference,<br />

which the this keyword actually represents, over to the Bagel class (object) using the Bagel() constructor method.<br />

Once the Bagel class has received this contextual information regarding how the InvinciBagel class (object) is set up,<br />

what it includes, and what it does (hey, I have not called this object reference a “contextual” object reference for no<br />

reason), it will be able to use the .isUp() method to “see” the value of a Boolean up variable, without having any static<br />

declaration in place anywhere other than for constants, which is what an import static reference should be used for.<br />

The first thing that we need to do to upgrade your Bagel class is to set up a variable to hold this InvinciBagel<br />

contextual object reference information, and modify our current Bagel() constructor method so that it can receive<br />

an InvinciBagel object reference. We’ll need to add a protected InvinciBagel invinciBagel; statement, at the top<br />

254<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!