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 16 ■ Collision Detection: Creating SVG Polygons for the Game Actors and Writing Code to Detect Collision<br />

When you test your collision code, you will find that you cannot get to some of these tiles! Let’s fix this next.<br />

Go into the Bagel.java class and modify the screen boundary constants at the top of the class so that the left and<br />

the top boundaries use the 0,0 origin values, since the Group class uses the upper-left corner of your screen as its<br />

point of reference, rather than the center of the screen, as the StackPane does. To calculate the rightBoundary value,<br />

you would take the WIDTH of the screen and subtract the width of the sprite using the SPRITE_PIXELS_X constant.<br />

A similar approach would be used for the bottomBoundary value, which will take the HEIGHT of the screen and<br />

subtract the height of the sprite using the SPRITE_PIXELS_Y constant. As you can see in Figure 16-45, we don’t have<br />

to change the .setBoundaries() method at all, thanks to the modular, logical and organized way that we have set up<br />

our code.<br />

Figure 16-45. Update the Bagel.java class with new Boundary values for the right, left, bottom and top Boundary value<br />

Before we finish up with the chapter, let’s add a scoring engine framework to our game, so that this collision<br />

detection routine calls a scoringEngine method, in addition to playing a sound and removing the Actor from the game.<br />

Creating a Scoring Engine Method: .scoringEngine( )<br />

Before we finish up with this chapter, let’s put a framework in place for the scoring engine, so we can focus the entire<br />

next chapter on game play. Create a private void scoringEngine(Actor object){} empty method right after the<br />

checkCollision() method, and add a scoringEngine(object); method call at the end of the if(collide(object))<br />

conditional if() structure, inside of the checkCollision() method. As you can see in Figure 16-46, you will get one wavy<br />

gray warning highlight under the Actor object reference inside of the .scoringEngine() method declaration. This is<br />

because the object reference has not been implemented inside of the method body.<br />

www.it-ebooks.info<br />

389

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

Saved successfully!

Ooh no, something went wrong!