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

Organizing the .update( ) Method: .moveInvinciBagel( )<br />

Since we are going to be adding more and more <strong>Java</strong> programming logic into the .update() method of the Bagel class<br />

during the remainder of the book, I want to put into place some “method modularization” that will be quite similar to<br />

what we did for the InvinciBagel class in Chapter 11 when we added the six new logical method structures. Since we<br />

will be performing a number of complex operations inside of the .update() method as the game becomes more and<br />

more complex, it is logical that the .update() method should contain calls to other methods that logically organize<br />

the tasks that we will need to do on each frame, such as determining keys pressed (or not pressed), moving the<br />

InvinciBagel character, looking to see if he has gone off the screen (setting boundaries), and eventually controlling<br />

his visual states, detecting collision, and applying physics effects. The first thing that I want to do is to “extract” the<br />

movement of the sprite into a .moveInvinciBagel() method that will perform any translation transforms that need to<br />

be implemented using a moveInvinciBagel(iX, iY); method call. This means that we will have to create the private<br />

void moveInvinciBagel(double x, double y){...} method structure and place the .setTranslate() method calls<br />

inside of it, replacing them in the .update() method with the .moveInvinciBagel() method call. The basic <strong>Java</strong> code to<br />

perform these changes to the Bagel.java class are shown in Figure 12-13, and will look like this <strong>Java</strong> code:<br />

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

moveInvinciBagel(iX, iY);<br />

}<br />

private void moveInvinciBagel(double x, double y) {<br />

spriteFrame.setTranslateX(x);<br />

spriteFrame.setTranslateY(y);<br />

}<br />

Figure 12-13. Create a .moveInvinciBagel() method for .setTranslate() method calls, and call if from .update() method<br />

Next, let’s move an iBagel on the screen using the ImageView .setTranslateX() and .setTranslateY() methods.<br />

www.it-ebooks.info<br />

265

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

Saved successfully!

Ooh no, something went wrong!