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

Game Design: Adding Our CastingDirector.java class<br />

The first thing that I want to do to is to update our invincibagel package and class structure diagram to show you<br />

the new actor (sprite) management class that we are going to develop during this chapter using <strong>Java</strong> ArrayList and<br />

HashSet classes (objects). As you can see in Figure 10-1, I am going to name this class CastingDirector.java because<br />

it will act just like a Casting Director would for any entertainment project, adding Actors to the project, and removing<br />

them when the scene is finished. This class will also contain a <strong>Java</strong> collection (a <strong>Java</strong> ListArray is an ordered collection<br />

and a <strong>Java</strong> HashSet is an unordered collection) that will be used when we start to implement collision detection later<br />

on during the book. As your game levels and scenes get more complicated, you will be glad to have a CastingDirector<br />

class that keeps your game actors organized and adds and removes actors from the game as needed by your game’s<br />

programming logic. It is important to keep track of exactly how many Actor (fixed sprites) and Hero (motion sprites)<br />

objects are in the scene, so that you only involve the fewest possible number of actors in your collision detection code<br />

(algorithms). This is a function of optimizing your game programming logic so the game plays well across all platforms.<br />

Figure 10-1. Create a CastingDirector.java actor casting and tracking engine to keep track of Actor and Hero objects<br />

Before we code our CastingDirector.java class, let’s take some time to learn about <strong>Java</strong> collections, generics, and<br />

the List, ListArray, Set and HashSet classes that we are going to use to create these actor management tools.<br />

List and ArrayList: Using java.util List Management<br />

First, let’s cover the public class ArrayList because it is a class, and then we will look at List as that is an Interface,<br />

and not a <strong>Java</strong> class. In case you are wondering what the stands for, it stands for Element, if you see a that<br />

stands for Key, if you see a that stands for Type, and if you see a that stands for Value. The gets replaced<br />

by an element (object) that you’re using in an ArrayList. In our case that is ArrayList as CastingDirector.java<br />

class ArrayList (and Set) will reference Actor objects (subclasses of Actor superclass). The class hierarchy is as follows:<br />

java.lang.Object<br />

> java.util.AbstractCollection<br />

> java.util.AbstractList<br />

> java.util.ArrayList<br />

208

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

Saved successfully!

Ooh no, something went wrong!