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

Figure 12-11. Change declaration statement for HBox object named buttonContainer from a static modifier to private<br />

I had originally declared these fields static because I had read on the Internet, on several of the <strong>Java</strong> coding sites,<br />

that this is an “optimization trick,” which allows the JVM to set aside fixed areas of memory, and make the code run<br />

more optimally. I have decided to make the objects for my game as encapsulated as possible first, here during the<br />

development process, and then look at optimization later on, if and when it becomes necessary.<br />

As long as we are making our InvinciBagel.java class completely encapsulated, let’s make the other variables<br />

private as well. I tested this game after making each of the variables (after the Bagel iBagel declaration) private, and it<br />

ran just fine. When I make the Bagel iBagel private, the game hangs on a white (background color) screen, so I left the<br />

Bagel iBagel declaration package protected (no access control modifier keyword signifies package protected access).<br />

Making the Remaining Variables Private: Finish Encapsulating InvinciBagel Class<br />

My work process for making the other eight lines of variable declarations private rather than package protected was<br />

to place the private <strong>Java</strong> access control keyword in front of the (simple or compound) variable declaration, and then<br />

use the Run ➤ Project work process (or click the green play icon at the top of NetBeans, which is faster) to test the<br />

code and see if all of the features, including the Button UI, splashscreens and character movement, work as they have<br />

been, using package protected access control. If for some reason at a later point in the software development we need<br />

www.it-ebooks.info<br />

263

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

Saved successfully!

Ooh no, something went wrong!