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 11 ■ Moving Your Action Figure in 2D: Controlling the X and Y Display Screen Coordinates<br />

Figure 11-13. Add four if statements to the .update() method, one for each right, left, down, and up boolean variable<br />

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

Moving a Scene Graph ImageView Node: .setTranslateX( ) and .setTranslateY( )<br />

Now that we have the conditional statements in place that will process where our InvinciBagel is supposed to be on<br />

the screen based on what arrow keys (or ASDW keys) are being held down (or not held down) by the player, let’s add<br />

the <strong>Java</strong> programming statements that will take this data from our InvinciBagel iX and iY variables, and pass this<br />

sprite location information to the spriteFrame ImageView Node object, to actually have it reposition the Node on the<br />

display screen. The .setTranslateX() and .setTranslateY() methods are part of the Node superclass’s transformation<br />

methods. These methods also include method calls that will rotate and scale a Node object; in this case, it is the Actor<br />

spriteFrame ImageView Node that contains one of the Image assets held in your List ArrayList object.<br />

When we call these .setTranslate() methods, off of our iBagel object’s spriteFrame ImageView Node object, we<br />

are referring to the spriteFrame ImageView object that we installed inside of the abstract Actor superclass. Since an<br />

Actor superclass was used to create the Hero superclass, which was used to create the Bagel class, the spriteFrame<br />

ImageView object can be referenced inside of the Bagel class by using a spriteFrame.setTranslateX(iX) statement, as<br />

is shown in the following <strong>Java</strong> code for the completed .update() method, which is also shown in Figure 11-14:<br />

public void update() {<br />

if(right) { iX += vX }<br />

if(left) { iX -= vX }<br />

if(down) { iY += vY }<br />

if(up) { iY -= vY }<br />

spriteFrame.setTranslateX(iX);<br />

spriteFrame.setTranslateY(iY);<br />

}<br />

www.it-ebooks.info<br />

245

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

Saved successfully!

Ooh no, something went wrong!