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

Since we are writing this code inside of the Bagel object (in our case, we instantiated and named it iBagel), we<br />

will have a chance to utilize and understand the iX and iY variables, which we will be doing in the next section of this<br />

chapter, when we develop the code statements that access and change the iBagel Bagel object’s iX and iY location<br />

attributes, as well as adding code that accesses and utilizes the iBagel Bagel object’s vX and vY velocity attributes.<br />

Building the .update() Method: Using If Statements to Determine X or Y Movement<br />

Now it is time to add some basic <strong>Java</strong> programming logic inside of the Bagel class .update() method that will move the<br />

iBagel object along the X or Y axis (or both, if multiple keys are being pressed). Since our iX and iY variables hold the<br />

Actor location on the screen, we will use these inside of each if statement, and add (or subtract) the velocity variable<br />

amount for each axis (vX if we are dealing with iX, vY if we are dealing with iY) respectively. We have initially set the<br />

vX and vY values at one, which would equate to a relatively slow movement. If the vX and vY were set to 2, the iBagel<br />

would move twice as fast (it would move by two pixels on each pulse event, instead of by one pixel).<br />

If the right Boolean variable is true, we want your iBagel object to move in the positive direction along the X axis,<br />

so we would use an if(right){iX+=vX} programming statement to add the vX velocity value to the iX location value<br />

using the += operator we learned about in Chapter 3. Similarly, if the left Boolean variable is true, we would use an<br />

if(left){iX-=vX} programming statement, which will subtract the vX velocity value from the iX location value, using<br />

the -= <strong>Java</strong> operator.<br />

We will do essentially the same thing along the Y axis when the up and down (or W and S) keys are pressed. If<br />

the down Boolean variable is true, we want the iBagel object to move in the positive direction along the Y axis. Thus<br />

we would use an if(down){iY+=vY} programming statement, which will add the vY velocity value to the iY location<br />

value, using the += operator. In <strong>Java</strong>FX, a positive X value goes from the 0,0 origin to the right, while positive Y values<br />

go from 0,0 down. Finally, to move the iBagel up, we will use an if(up){iY-=vY} programming statement, which will<br />

subtract the vY velocity value from the iY location value, using the -= operator. The basic <strong>Java</strong> code to perform these<br />

four conditional if statement evaluations, and their respective X or Y sprite movement calculations inside of the Bagel<br />

class .update() method, is shown in Figure 11-13, and should look like the following method body structure thus far:<br />

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

}<br />

244<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!