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 10 ■ Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class<br />

If there are multiple Actor objects to be processed the inside of an if{...} construct, use the .addAll() method to<br />

add the contents of the parameter list to your REMOVED_ACTORS Set object. This is accomplished by using<br />

the Arrays.asList((Actor[]) actors)) construct inside of the .addAll() method call, which constructs the Actor[]<br />

Array named actors that is compatible with (necessary for) using an .addAll() method call with a Set object type.<br />

The second else{...} portion of this method body adds one single Actor object, since the actors.length was not greater<br />

than one, by using an actors[0] annotation (first Actor parameter) and an .add() method call using the following code:<br />

public void addToRemovedActors(Actor... actors) {<br />

if (actors.length > 1) { REMOVED_ACTORS.addAll(Arrays.asList((Actor[]) actors)); }<br />

else { REMOVED_ACTORS.add(actors[0]); }<br />

Notice that since we have converted the Actor... parameters (which are destined for a List, but are not one yet)<br />

into an Array (because the compiler can count the fixed number of items), so we can use the actors[0] notation.<br />

Now that we have a way to add one or more Actor objects to the REMOVED_ACTORS Set HashSet, let’s<br />

create a .resetRemovedActors() to clear out the REMOVED_ACTORS data set. Before we clear the Set object<br />

we need to make sure all of the Actor objects contained within it are removed from the CURRENT_CAST Actor List<br />

object, since that is what it is there for, so the first part of this method will call the .removeAll() method off of the<br />

CURRENT_CAST ArrayList object and inside of this method pass over the REMOVED_ACTORS Set<br />

object. After that we can use the .clear() method call off of the REMOVED_ACTORS object to reset it back to being<br />

empty, so that it can be used all over again to collect Actor objects that need to be disposed of. The <strong>Java</strong> code, which is<br />

shown in Figure 10-9, should look like the following:<br />

public void resetRemovedActors() {<br />

CURRENT_CAST.removeAll(REMOVED_ACTORS);<br />

REMOVED_ACTORS.clear();<br />

}<br />

Next, we are going to look at how we can get NetBeans to code our CastingDirector() constructor method!<br />

CastingDirector() Constructor: Having NetBeans Write the Code<br />

There is a way that you can get NetBeans to write a constructor method for you, and since it is a little bit “hidden,”<br />

I’ll show you how to find it! I left the insertion-bar cursor in Figure 10-10, to show you that I clicked on the final<br />

keyword and the yellow light bulb “tip” icon that appears, and the pale yellow pop-up tooltip message that I get when<br />

I mouse-over the tip light bulb. The message that I get is the “Move initializer to constructor(s),” and so I hit the<br />

Alt-Enter key combo that is suggested. Sure enough, there is an option for NetBeans to write this constructor method<br />

code for me.<br />

www.it-ebooks.info<br />

221

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

Saved successfully!

Ooh no, something went wrong!