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

Creating an ArrayList Object: CURRENT_CAST Data Store List<br />

The first thing that we need to add to the CastingDirector class is a private List ArrayList object that<br />

I am going to name CURRENT_CAST as it contains the Actor objects that are currently on the Stage, which is the<br />

current cast. Although it is not technically a constant as far as using static and final keywords in its declaration, it<br />

is acting (no pun intended) as a database of sorts, and so I am using ALL_CAPS so that it stands out in the code as<br />

being a data structure. I’m also going to add a basic .get() method to access the ArrayList structure using<br />

a <strong>Java</strong> return keyword to return an object to the calling entity. The code for the declaration and instantiation of<br />

the CURRENT_CAST ArrayList object and the body of the .getCurrentCast() method structure should look like the<br />

following <strong>Java</strong> code:<br />

package invincibagel;<br />

public class CastingDirector {<br />

private List CURRENT_CAST = new ArrayList();<br />

}<br />

public List getCurrentCast() {<br />

return CURRENT_CAST;<br />

}<br />

As you can see in Figure 10-3, there is wavy red error highlighting under your List interface reference as well as<br />

your ArrayList reference in the line of code that declares and instantiates the CURRENT_CAST object, so you’ll need<br />

to use the Alt-Enter work process, and have NetBeans 8 write the import java.util.List; statement at the top of<br />

your class. The .getCurrentCast() will be the easiest method to code, as it simply returns your entire CURRENT_CAST<br />

ArrayList object to whatever <strong>Java</strong> entity may be calling the method. Next, we’ll take a look at how to code<br />

the more complicated ArrayList data store access methods, which will deal with adding, removing, and resetting<br />

(clearing) the Actor objects from this CURRENT_CAST ArrayList object.<br />

Figure 10-3. Inside the CastingDirector class, add a CURRENT_CAST List object, and a .getCurrentCast() method<br />

The first method that we will code is the .addCurrentCast() method, which will pass in a comma delimited List<br />

of Actor objects to the List (and the ArrayList class that implements List) interfaces .addAll() method call. As you have<br />

learned already, a comma delimited List is passed at the end of the method parameter list, unless, as it is in this case,<br />

it is the only parameter.<br />

214<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!