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 />

of the class, to create an invinciBagel reference object (the variable will hold a reference to this object in memory) to<br />

hold this information. The reason I am making this protected access is so that if we make any subclass using Bagel, it<br />

will have access to this contextual object reference information. This object declaration would use the following <strong>Java</strong><br />

statement, located at the very top of the Bagel.java class, as shown in Figure 12-4:<br />

protected InvinciBagel invinciBagel;<br />

Figure 12-4. Add an InvinciBagel object variable named invinciBagel, and add the object into the constructor method<br />

Next, let’s add the InvinciBagel context object into the front of the Bagel() constructor’s parameter list, since we<br />

can’t put it at the end of the parameter list, because we’re using the end of the parameter list to hold our Image... List<br />

(or Array, at some point in the code it is both of these) specification. Inside of the constructor method itself, you’ll set<br />

the InvinciBagel reference object, which is passed into the constructor method using the name iBagel, to equal an<br />

invinciBagel variable, which you have already declared at the top of the Bagel.java class. This would all be done using<br />

the following modified Bagel() constructor method structure, which can be seen highlighted at the top of Figure 12-4:<br />

public Bagel(InvinviBagel iBagel, String SVGdata, double xLocation, double yLocation,<br />

Image... spriteCels) {<br />

super(SVGdata, xLocation, yLocation, spriteCels);<br />

invinciBagel = iBagel;<br />

}<br />

As you can see in Figure 12-4, our code is error-free, and we are ready to go back into our InvinciBagel.java class<br />

and add the <strong>Java</strong> this keyword into the Bagel() constructor method call. Doing this will pass an InvinciBagel class<br />

(object) reference object over to the Bagel.java class (object) so that we will be able to use the .is() and .set() methods<br />

from the InvinciBagel class without having to specify any import statements whatsoever. You can also delete the four<br />

import static statements at the top of your Bagel.java class. As you can see in Figure 12-4, I have deleted these static<br />

import statements already.<br />

Now let’s go back into the InvinciBagel.java NetBeans editing tab, and finish wiring the two classes together.<br />

www.it-ebooks.info<br />

255

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

Saved successfully!

Ooh no, something went wrong!