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

Figure 16-29. Remove the iBagel object from the front of the list of objects passed into the .addCurrentCast() method<br />

Now we are ready to start to put the methods into place in the Bagel.java class that will check the iBagel object<br />

for collision against the objects which are in the CURRENT_CAST List object by checking the boundaries for<br />

each object’s Node (ImageView), and collision Shape (SVGPath). What we will be covering next can get somewhat<br />

complicated, but if you want to really create a legitimate game title, you need to be able to know when the game play<br />

elements intersect (called collide in game development terminology) with each other. Let’s dive into that Abyss now!<br />

Bagel Class Collision Detection: .checkCollision( )<br />

Right-click on the Bagel.java class in the Projects pane shown in Figure 16-29 and select the Open option and open up<br />

the Bagel.java tab in NetBeans 8. Go into the .update() method, and comment out the playAudioClip() method call,<br />

using two forward slashes at the beginning of the line of code. This will effectively turn off the audio for the KeyEvent<br />

(KeyCode) KeyPressed constant call-outs that we have in place. The reason I am doing this is because I am going to<br />

use this audio to “call out” a detected collision in the code that we’re about to create. Next, create a .checkCollision()<br />

method call at the end of the methods that are called inside of the .update() method. This method will call collision<br />

detection <strong>Java</strong> code, which we are about to put into place during the remainder of this chapter, on every pulse event<br />

that is processed. This is in keeping with the logical sequence of game code processing inside of the .update() method.<br />

The new .update() method, which is seen in Figure 16-30, should look like the following <strong>Java</strong> method body:<br />

public void update() {<br />

setXYLocation();<br />

setBoundaries();<br />

setImageState();<br />

moveInvinciBagel();<br />

// playAudioClip();<br />

checkCollision();<br />

}<br />

370<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!