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

As you can see in Figure 12-6, the code is error-free, and you now have an .update() method that accesses the<br />

Boolean variables from your InvinciBagel.java class without having to use any import static statements to do so.<br />

You might be thinking, well, since this is a great way to get rid of import static statements in my Bagel.java class,<br />

why don’t I use this same approach to also get rid of the static Bagel iBagel declaration in my InvinciBagel.java class,<br />

as well as the import static statement that is used in the GamePlayLoop.java class to access the static iBagel Bagel<br />

object? Wow, that is a fantastic idea, folks, I just wish that I had thought of it! In fact, let’s do that right now!<br />

Removing a Static iBagel Reference: Revise the Handle( ) Method<br />

As you can see in Figure 12-7, we still have quite a few InvinciBagel variables declared using the static keywords that<br />

are not, in fact, constants. Before this chapter is over we will have eliminated these, so that only our WIDTH and<br />

HEIGHT constants use the static modifier keyword. Since we are going to pass the InvinciBagel object reference<br />

to the GamePlayLoop class using the <strong>Java</strong> this keyword inside of the GamePlayLoop() constructor method, which<br />

means that we can remove the static keyword from the Bagel iBagel object declaration statement at the top of the<br />

InvinciBagel class. This can be accomplished using the following variable declaration, which is shown (highlighted)<br />

in Figure 12-7:<br />

Bagel iBagel;<br />

Figure 12-7. Remove the <strong>Java</strong> static modifier keyword from in front of your Bagel iBagel object declaration statement<br />

The next thing that we’ll need to do to makes sure that our InvinciBagel and GamePlayLoop classes (objects) can<br />

talk to each other is to make the GamePlayLoop() constructor method compatible with (accept in the InvinciBagel<br />

this context reference object inside of its parameter list) the InvinciBagel class’s this object reference that we need to<br />

send over to the GamePlayLoop class inside of the constructor method call. Since we are currently depending on the<br />

<strong>Java</strong> compiler to create the GamePlayLoop() constructor method for us, we will need to create one for ourselves! As<br />

you learned in Chapter 3, if you do not explicitly create a constructor method for a class, one will be created for you.<br />

258<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!