28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Modifying the iBagel Instantiation: Adding a <strong>Java</strong> this Keyword to the Method Call<br />

Open up your .createGameActors() method structure using the + expand icon on the left side of NetBeans. Add a<br />

this keyword at the “head” or front of the list of parameters that you are passing into the Bagel() constructor method call.<br />

Your newly revised <strong>Java</strong> statement should look like the following code, which is also shown highlighted in Figure 12-5:<br />

iBagel = new Bagel(this, "M150 0 L75 200 L225 200 Z", 0,0, iB0,iB1,iB2,iB3,iB4,iB5,iB6,iB7,iB8);<br />

Figure 12-5. Modify the iBagel instantiation to add a <strong>Java</strong> this keyword to the Bagel() constructor method parameters<br />

As you can see in Figure 12-5, your <strong>Java</strong> code is error-free, which means that you have now passed a copy of your<br />

InvinciBagel.java class’s (or the object created by it, however you prefer to look at it) context into the Bagel class (or<br />

more precisely, into the object which is created by your use of the Bagel() constructor method). What is contained<br />

in the digital context structure of the this object is beyond the scope of a beginner book, but suffice it to say that the<br />

this keyword will pass over a complete structural reference to an object. This reference contains all of the contextual<br />

information which would be needed to give the object that it is being passed to enough information to be able to put<br />

everything into “digital perspective” (context) regarding the class that is passing the this reference object over, in our<br />

case, this will be the InvinciBagel class (object) passing contextual information about itself to the Bagel class (object).<br />

This will include your object structure (variables, constants, methods, etc.) for the object as well as state information<br />

relating to more complex things that relate to use of system memory and thread usage. Think of passing one object’s<br />

context information to another object using the <strong>Java</strong> this keyword as wiring them together, so to speak, so that your<br />

receiving object can peer into the sending object by using the this object reference.<br />

Now that the this reference (contextual object) for the InvinciBagel class (object) has been passed to the Bagel<br />

class (object) by using the <strong>Java</strong> this keyword inside of the Bagel() constructor method, we have now create a far more<br />

industry standard link between these two classes. We can now proceed to change our Bagel .update() method so that<br />

it uses the new .is() method calls to get the four different Boolean variable values (states) from the InvinciBagel object<br />

reference that it now has. We will need this data to be able to move our InvinciBagel character around on the screen.<br />

256<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!