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 12 ■ Setting Boundaries for Your Action Figure in 2D: Using the Node Class LocalToParent Attribute<br />

Setting Screen Boundaries: .setBoundaries( ) Method<br />

The next most important thing to do for our InvinciBagel game is to make sure that the character does not disappear<br />

off the edge of the screen by placing a .setBoundaries() method call between the .setXYLocation() method, which<br />

evaluates arrow (or ASDW) keypress combinations, and increments the iX and iY Bagel object properties accordingly,<br />

and the .moveInvinciBagel() method, which actually executes the movement. By placing the .setBoundaries() method<br />

before the sprite movement is invoked, we can make sure that the sprite is not off the screen (and if he is, move him<br />

back onto the screen) before we actually call the move function (method). The first step in writing this code is to<br />

define the sprite size in pixels so that we can calculate this along with our WIDTH and HEIGHT Stage size constants to<br />

determine the boundary variable values that we will need to check our iX and iY sprite location against inside of the<br />

.setBoundaries() method and its conditional if() statement structures. As you can see in Figure 12-15, I define these<br />

sprite pixel size constant declarations at the top of the Bagel.java class, by using the following two lines of <strong>Java</strong> code:<br />

protected static final double SPRITE_PIXELS_X = 81;<br />

protected static final double SPRITE_PIXELS_Y = 81;<br />

Figure 12-15. Declare protected static final double SPRITE_PIXELS_X and SPRITE_PIXELS_Y constants at the top of class<br />

Next, we need to calculate the four screen boundary values using the WIDTH and HEIGHT constants in the<br />

InvinciBagel class and the SPRITE_PIXELS_X and SPRITE_PIXELS_Y constants we just defined at the top of this class.<br />

As you may have noticed from our 0,0 initial X,Y Bagel object location coordinates putting our sprite in the center of the<br />

screen, <strong>Java</strong>FX is using a centered X axis and Y axis screen addressing paradigm. This means there are four quadrants,<br />

and that negative values (which mirror positive values) move left and up, and positive values move right and down.<br />

We can actually use this paradigm later on to quickly ascertain which quadrant of the screen the character is in. The<br />

way we would thus calculate the boundaries is to take half of the screen width and subtract half of the sprite width to<br />

find the right (positive) boundary value and simply take the negative of this for the value for the left boundary limit. A<br />

similar calculation applies to the top and bottom boundary value limit, for which we will take half of the screen height<br />

and subtract half of the sprite height to find the bottom (positive) boundary value and simply take the negative of this<br />

for the value for the top boundary limit value. The <strong>Java</strong> code for these calculations should look like the following:<br />

protected static final double rightBoundary = WIDTH/2 - SPRITE_PIXELS_X/2;<br />

protected static final double leftBoundary = -(WIDTH/2 - SPRITE_PIXELS_X/2);<br />

protected static final double bottomBoundary = HEIGHT/2 - SPRITE_PIXELS_Y/2;<br />

protected static final double topBoundary = -(HEIGHT/2 - SPRITE_PIXELS_Y/2);<br />

www.it-ebooks.info<br />

267

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

Saved successfully!

Ooh no, something went wrong!