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

}<br />

@Override<br />

public boolean collide(Actor object) {<br />

if (invinciBagel.iBagel.spriteFrame.getBoundsInParent().intersects(<br />

object.getSpriteFrame().getBoundsInParent() ) ) {<br />

Shape intersection =<br />

SVGPath.intersect(invinciBagel.iBagel.getSpriteBound(), object.getSpriteBound());<br />

if (intersection.getBoundsInLocal().getWidth() != -1) {<br />

return true;<br />

}<br />

}<br />

return false;<br />

}<br />

Figure 16-41. Optimizing the interaction between the collide() method and the checkCollision() method<br />

Use the Run ➤ Project work process to make sure the code works as it did before the method optimizations.<br />

Optimizing the Scene Graph: Using the Group Class<br />

Since we’re not really using the StackPane for image layer compositing or UI design, other than the splashscreen use,<br />

let’s add another optimization, and use a Group class (object) for our game, since it is further up the class hierarchy,<br />

and closer to the Node superclass than the more specialized StackPane class (object) is. Rather than using a class that<br />

uses the more complex Object ➤ Node ➤ Parent ➤ Region ➤ Pane ➤ StackPane hierarchy, we will use the much<br />

shorter and simpler Object ➤ Node ➤ Parent ➤ Group hierarchy. The significance of this optimization is that using a<br />

Group root Node uses a lot less code (properties and methods) in system memory. Since we are not specifically using<br />

any of the highly specialized StackPane properties or methods, we can perform a major optimization by using a Group<br />

class. A Group object (class) uses fixed object positioning, so we will have to redo the code for our HBox and Actor<br />

386<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!