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

Setting Boundaries for Your Action<br />

Figure in 2D: Using the Node Class<br />

LocalToParent Attribute<br />

Now that we have organized your <strong>Java</strong> code into logical methods in the InvinciBagel.java class, and wired up the<br />

GamePlayLoop .handle() method to the Bagel .update() method in Chapter 10, to make sure that our KeyEvent<br />

handlers will move our InvinciBagel character around the screen, it is time to establish some boundaries for our<br />

game hero, so that he does not leave the field of play, so to speak. To do this, we will need to dive into the <strong>Java</strong>FX Node<br />

superclass at a much deeper level of detail then we did in Chapter 4. We will look at how transforms are performed,<br />

and more important, how they function relative to the Parent Node, which is located above them in the Scene Graph.<br />

For our Actor ImageView Node(s), that Parent Node would be the Scene Graph root StackPane Node.<br />

Before we start getting into code complexities such as absolute or relative transformation, which we will be<br />

looking at during this chapter, and things such as collision detection and physics simulation, which we will cover later<br />

in Chapters 16 and 17, we will need to get back into our InvinciBagel.java primary application class <strong>Java</strong> code here in<br />

Chapter 12, so that we can do a few more things that will optimize the <strong>Java</strong> 8 foundation for our game. We have been<br />

putting our game engines into place during the first part of this book, and I want to make sure that everything is “up to<br />

snuff” before I start to build complex code structures on top of what we have put into place so far. We’re going to make<br />

sure everything is “locked down tightly!”<br />

For this reason, I am going to spend the first few pages of this chapter getting rid of those import static <strong>Java</strong><br />

statements, which although they work just fine, as you have seen, they are not what is termed “best practice” in <strong>Java</strong><br />

programming. There is a more complex and involved way to talk between classes, involving a <strong>Java</strong> “this” keyword, so I<br />

am going to show you how to implement far more private variables (and far less static variables), and then I will teach<br />

you how to use reference objects, represented by the <strong>Java</strong> this keyword, to send object data variables between classes.<br />

This is a somewhat advanced topic for a beginner level book, but it will allow you to write more professional<br />

and “industry standard” <strong>Java</strong> 8 code, so it will be worth the extra effort. Sometimes, the right way to do things is more<br />

involved and detailed than the basic (simple) way to get things coded. The assumption here is that you are going to be<br />

producing a commercially viable game, so you will need a rock solid foundation to build increasingly complex code on.<br />

After we finish adding additional code refinements in our InvinciBagel.java class, which will implement <strong>Java</strong><br />

“encapsulation” using private variables wherever possible, and the this keyword where needed to provide access to<br />

the InvinciBagel object to other related classes—in this case, the GamePlayLoop and Bagel classes for now—we will<br />

begin to add complexity to the sprite movement code that is in our Bagel class .update() method.<br />

We will add code that will tell your InvinciBagel character where the ceiling and floor of his Scene and Stage are<br />

located, and where the left and right sides of the screen are, so that he does not fall off of his flat 2D world. We will<br />

also organize the methods in the Bagel.java class, so that the .update() method only calls higher-level methods that<br />

contain all of the <strong>Java</strong> programming logic in an exceptionally well organized fashion.<br />

www.it-ebooks.info<br />

251

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

Saved successfully!

Ooh no, something went wrong!