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

As you can see in Figure 12-8, I have clicked on the invinciBagel variable (InvinciBagel object reference) so it is<br />

highlighted, and you can see its usage across the two methods. The declaration is used in the GamePlayLoop class,<br />

the instance inside of the GamePlayLoop() constructor method is set using the InvinciBagel class this keyword (using<br />

the iBagel parameter), and the variable reference inside of the .handle() method accessed the Bagel class .update()<br />

method using the iBagel Bagel object and the invinciBagel InvinciBagel reference object. <strong>Java</strong> is advanced, but cool.<br />

Now that we have created our custom GamePlayLoop() constructor method that accommodates the receipt of<br />

the InvinciBagel object reference named iBagel, and then assigns it to the invinciBagel variable, it is time to return to<br />

the InvinciBagel.java code (editing tab in NetBeans).<br />

The final piece of this (second) puzzle of removing the static Bagel iBagel; declaration is to add the <strong>Java</strong><br />

this keyword in the GamePlayLoop() constructor method call. After we do this, our InvinciBagel, Bagel and<br />

GamePlayLoop will all be wired up to each other, without using any static variables (other than the WIDTH and<br />

HEIGHT constants).<br />

Using this in GamePlayLoop() Constructor: GamePlayLoop(this)<br />

Open up your .createStartGameLoop() method structure, using the + expand icon on the left side of NetBeans, as<br />

shown in Figure 12-9. Add the <strong>Java</strong> this keyword in the parameter area, so that you are again passing the InvinciBagel<br />

object reference, this time into the GamePlayLoop() constructor method call. This will give that class a reference to<br />

your InvinciBagel class and its context and structure, just like you did with the Bagel class. Your newly revised <strong>Java</strong><br />

method body and constructor method call will look like the following code, which is shown highlighted in Figure 12-9:<br />

private void createStartGameLoop() {<br />

gamePlayLoop = new GamePlayLoop(this);<br />

gamePlayLoop.start();<br />

}<br />

260

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

Saved successfully!

Ooh no, something went wrong!