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

Next, let’s test all of this code to see if it does what we think that it logically should do! The code is quite well<br />

organized and very logical, so I don’t see any problems with it, but testing it in NetBeans is the only real way to find<br />

out for sure! Let’s do that next. This is getting kind of exciting!<br />

Testing the InvinciBagel Sprite Boundaries: Run ➤ Project<br />

Now it is time to use the NetBeans Run ➤ Project work process and test the .setBoundaries() method, which now gets<br />

called after the .setXYLocation() method but before the .moveInvinciBagel() method. So the logical progression as it<br />

sits now is check keypressed and set X and Y location based on that, then check to make sure you have not gone past<br />

any boundaries, then position the sprite.<br />

As you can see in Figure 12-19, the InvinciBagel character now stops at all four edges of the screen. On the<br />

left and right sides he stops a short distance away from the side of the screen because the sprite is centered in the<br />

ImageView area, but once we get him running, which we will be doing in the next chapter covering how to animate<br />

the character’s movements, this will look at lot closer to the edge of the screen. We always have the option to adjust<br />

our leftBoundary and rightBoundary variable algorithms at the top of the Bagel.java class, which allows us to “tweak”<br />

the boundary limits value later on, as we continue to refine our code.<br />

Figure 12-19. Testing the InvinciBagel character movement; shown as stopping at the top and bottom boundary limits<br />

Now that we have both organized and encapsulated our code, got the sprite movement working and set the<br />

boundaries for the edges of the screen, we can start to look at implementing the different sprite image states so that<br />

when combined with the key movement, we can start to create a more realistic InvinciBagel character action figure!<br />

Summary<br />

In this twelfth chapter, we privatized our primary InvinciBagel.java class as much as possible, and removed all of the<br />

static modifier keywords that were not specifically related to constants (WIDTH and HEIGHT). First we removed<br />

the public static Boolean variables and made them private to the InvinciBagel class, and then created getter and<br />

setter methods to allow the Bagel class to use these variables using .is() method calls. We also had to pass the<br />

InvinciBagel object reference to the Bagel object inside of the front of the Bagel() constructor parameter list using<br />

the <strong>Java</strong> this keyword. We made these same changes to the static Bagel iBagel object declaration, removing the static<br />

modifier keyword and passing the InvinciBagel object context using the <strong>Java</strong> this keyword, this time inside of the<br />

GamePlayLoop() constructor method call. To do this we had to create our own custom GamePlayLoop() constructor<br />

method rather than using the one that is created by the compiler (JVM) if we do not specifically provide one.<br />

270

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

Saved successfully!

Ooh no, something went wrong!