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

Not only do we need to remove this collided Actor object from the game cast using the CastingDirector class<br />

(object), we also need to remove the ImageView Node from the Scene Graph root as well. Let’s look at how to do that<br />

next, and then we will take a look at how to remove the Actor object from the CURRENT_CAST List after that.<br />

Removing Actors from the Scene Graph: .getChildren().remove()<br />

Now that we have effectively removed the Actor that the InvinciBagel collided with from the cast by adding it to the<br />

REMOVED_ACTORS HashSet object, the next step is to remove it from the <strong>Java</strong>FX Scene Graph, which we<br />

are currently using the StackPane UI layout container class for. If you remember earlier in the book, we learned that<br />

the StackPane class can be used as a layer-based layout container, and arranges its contents using a centered XY grid.<br />

We will be learning how to use a Group object (class) as our Scene Graph root later on in the chapter, to implement<br />

the more typical upper-left corner 0,0 XY location indexing scheme and as an optimization, as we are not specifically<br />

using any of the StackPane class properties or methods, and because a Group class is a more basic class that is higher<br />

up in the Node superclass hierarchy. In this way, you will have experienced using both center screen 0,0 location<br />

referencing (StackPane), as well as the traditional upper-left 0,0 screen location referencing (a Group class as a Scene<br />

Graph root).<br />

Removing a Node object from the Scene Graph root is done using a .getChildren().remove() method chain, in<br />

the exact opposite fashion of the .getChildren.add() method call found in the .addGameActorNodes() method. Add a<br />

line of code, and type invinciBagel.root and a period, as shown in Figure 16-35, to open the method helper pop-up.<br />

Figure 16-35. Add a line of code under an .addToRemovedActors() method call and type invinciBagel.root and a<br />

period<br />

Since we are doing this ImageView Node removal from the Bagel.java class, we will use <strong>Java</strong> dot notation to<br />

preface the root object with the invinciBagel object reference before we add the .getChildren() method call, using<br />

the following invinciBagel.root.getChildren() code shown in Figure 16-35. Double-click on the remove(Object o)<br />

selection and add the object you passed into the .collide() method into the .remove() method, as seen in Figure 16-36.<br />

This will create the final invinciBagel.root.getChildren().remove(object); <strong>Java</strong> programming statement.<br />

380<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!