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

Using Your New InvinciBagel .is( ) Methods: Updating Your Bagel .update( ) Method<br />

The next step in our elimination of using import static references and static variables will be to rewrite the conditional<br />

if statements using the .isUp(), isDown(), isLeft() and isRight() method calls. Since we’re not using static variables to<br />

reach across classes (objects) anymore, we’ll need to replace these actual up, down, left, and right static variables that<br />

are currently used inside the if() statements in the Bagel class .update() method. These will no longer work, because<br />

they are now encapsulated in the InvinciBagel class, and are private variables, so we will have to use .isUp(), isDown(),<br />

isLeft(), and isRight() “getter” methods instead, to politely knock on the InvinciBagel’s door, and ask for these values!<br />

We will call our four .is() methods “off of” the InvinciBagel reference object (using dot notation), which we have<br />

declared and named invinciBagel at the top of the Bagel.java class. This variable (object reference) contains the<br />

InvinciBagel class context, which we sent from the InvinciBagel class into the Bagel class using the <strong>Java</strong> this keyword.<br />

What this means is that if we say invinciBagel.isRight() in our code, our Bagel class (object) now knows that to mean:<br />

go into the invinciBagel InvinciBagel object using “this” reference object (just trying to be cute here), which will now<br />

show the Bagel class (object) how, and where, to get to, and to execute, the public void .isRight() {...} method<br />

structure, which will pass over the private boolean right variable encapsulated in an InvinciBagel object. This is<br />

included here as a demonstration of the <strong>Java</strong> OOP concept of “encapsulation.”<br />

Your new .update() method body will use the same six lines of <strong>Java</strong> code, modified to call the .is() methods on the<br />

inside of the if(condition=true) evaluation portion of your existing conditional if structure. The new <strong>Java</strong> code, which<br />

is also shown in Figure 12-6, should look like the following:<br />

public void update() {<br />

if(invinciBagel.isRight()) { iX += vX; }<br />

if(invinciBagel.isLeft()) { iX -= vX; }<br />

if(invinciBagel.isDown()) { iY += vY; }<br />

if(invinciBagel.isUp()) { iY -= vY; }<br />

spriteFrame.setTranslateX(iX);<br />

spriteFrame.setTranslateY(iY);<br />

}<br />

Figure 12-6. Insert the invinciBagel.is() method calls inside of the if statements, where the boolean variables used to be<br />

www.it-ebooks.info<br />

257

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

Saved successfully!

Ooh no, something went wrong!