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

InvinciBagel Privatization: Removing Static Modifiers<br />

The first thing that I want to do regarding the InvinciBagel.java class <strong>Java</strong> code, as well as the GamePlayLoop.java class<br />

code and the Bagel.java class code that both reference the InvinciBagel up, down, left, and right Boolean variables<br />

by using import static statements, is to remove these import static statements from the top of both of these “worker”<br />

classes, and instead pass an InvinciBagel class (context) object, using the <strong>Java</strong> this keyword, in the Bagel() constructor<br />

method as well as in the GamePlayLoop() constructor method. The first step in this process, which will span the next<br />

several pages of the chapter, is to change the public static boolean variable declaration compound statement to not<br />

use a static modifier, and instead of the public access control modifier, to use the private keyword, as is shown here:<br />

private boolean up, down, left, right;<br />

As you can see in Figure 12-1, this doesn’t generate any red errors or yellow warning highlights in the code;<br />

however, it does generate wavy grey underlining. This signifies that the code that is highlighted is not currently being<br />

used. Since the “convention” or general rule in <strong>Java</strong> regarding static modifier keywords is to use them with constants,<br />

such as we are in the first line of code, and thus, I am going to try and “encapsulate” the code in this InvinciBagel.java<br />

class as much as possible, by removing the static modifiers (first), and making many of the other declarations private.<br />

Figure 12-1. Change the public static access modifiers for the boolean KeyEvent variables to be private access control<br />

The code that we are going to put into place next will eliminate this wavy grey highlighting, and in fact, we are<br />

going to have NetBeans write that code for us using the Source ➤ Insert Code ➤ Getters and Setters work process,<br />

which we learned about in Chapter 8, when we created our Actor and Hero superclasses. The .is() and .set() methods,<br />

which we are about to generate next, are the solution that allows us to eliminate the public static variable declaration<br />

that was allowing your classes external to InvinciBagel.java to “reach inside” (think of this as a security breach) to grab<br />

these four Boolean variables. Making these variables private prevents this. So, we need to put .is() and .set() methods<br />

into place which force external classes and methods to “request” this information, using a more “formal” method call.<br />

252<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!