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

To show the .addCurrentCast() method that we are going to pass more than one Actor object into the body of the<br />

method, we used the Actor... annotation, and I am going to name the (more than one) Actor objects variable actors.<br />

Inside of the body of the .addCurrentCast() method we will call an .addAll() method off of the CURRENT_CAST<br />

object using dot notation.<br />

Inside of the .addAll() method we will nest another <strong>Java</strong> statement that will create an Arrays object from the<br />

comma delimited List using the .asList() method called off the Arrays class reference and pass the actors Actor...<br />

comma delimited list into that method. This is all done using the following <strong>Java</strong> method construct:<br />

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

CURRENT_CAST.addAll( Arrays.asList(actors) );<br />

}<br />

As you can see in Figure 10-4, you will get a wavy red error highlighting under the Arrays class, so use the<br />

Alt-Enter work process and have NetBeans write your import java.util.Arrays; statement for you. Now we are<br />

ready to write the other two methods relating to the CURRENT_CAST data store that will remove Actor objects from<br />

the List ArrayList data store object, and one that will clear it out entirely (reset it to being unused).<br />

Figure 10-4. Add .addCurrentCast(), removeCurrentCast(), and resetCurrentCast methods to the CastingDirector class<br />

The second method that we will code is the .removeCurrentCast() method, which will also pass in a comma<br />

delimited List of Actor objects to the List (and ArrayList class implementing List) interfaces .removeAll() method call.<br />

To show this .removeCurrentCast() method that we are going to pass more than one Actor object into the body<br />

of the method, we again use the Actor... annotation, and I am going to again name this variable actors. Inside of the<br />

body of the .removeCurrentCast() method we will again call the .removeAll() method off of the CURRENT_CAST<br />

object, and inside of the .removeAll() method we’ll nest another <strong>Java</strong> statement that will create an Arrays object from<br />

www.it-ebooks.info<br />

215

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

Saved successfully!

Ooh no, something went wrong!