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

As you can see in Figure 12-17, this will generate an error under the method call, until we code the method.<br />

Create a private void setBoundaries(){...} empty method structure underneath the .setXYLocation()<br />

method structure, so that your methods are in the same order that we will call them from inside of your .update()<br />

method. Next you will place your four conditional if() structures, one for each screen boundary, starting with the X<br />

axis related right and left screen boundaries, and then for the Y axis related bottom and top screen boundaries. The<br />

first if statement needs to look at the rightBoundary value and compare the current iX location to that value. If the iX<br />

value is greater than or equal to the rightBoundary value limit, then you want to set the iX value to the rightBoundary<br />

value. This will keep the InvinciBagel locked into position right at the boundary. The reverse of this logic will also<br />

work for the left side of the screen; if the iX value is less than or equal to the rightBoundary value limit, then you will<br />

want to set the iX value equal to the leftBoundary value.<br />

The third if statement needs to look at the bottomBoundary value and compare the current iY location to that<br />

value. If the iY value is greater than or equal to the bottomBoundary value limit, then you will want to set the iY<br />

value to the bottomBoundary value. This will keep your InvinciBagel locked into position at the bottom of the screen<br />

boundary. The reverse of this logic will also work for the top of the screen; if the iY value is less than or equal to the<br />

topBoundary value limit, then you will want to set the iY value equal to the topBoundary value. The <strong>Java</strong> code for the<br />

.setBoundaries() method including the four if() statements is shown in Figure 12-18, and should look like the following:<br />

private void setBoundaries() {<br />

if (iX >= rightBoundary) { iX=rightBoundary; }<br />

if (iX = bottomBoundary) { iY=bottomBoundary; }<br />

if (iY

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

Saved successfully!

Ooh no, something went wrong!