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

Directing the Cast of Actors: Creating<br />

a Casting Director Engine and<br />

Creating the Bagel Actor Class<br />

Now that we have created the public abstract Actor and Hero classes (the Actor Engine) in Chapter 8, and some basic<br />

KeyEvent handling in Chapter 9, it is time to put more of our game infrastructure in place here in Chapter 10. We are<br />

going to create yet another <strong>Java</strong> class to manage our cast of actors, called CastingDirector.java (the Casting Engine).<br />

We will do this so that we can keep track of the Actor objects on the game play screen that have been created using our<br />

Actor and Hero abstract classes. It is important to know what game components (actors) are currently on the screen<br />

(or stage, or set, if you like the film production lingo that we are using) at any given time (or level) in the game.<br />

In this chapter, we’ll also need to learn more about, and use, the List and ArrayList classes as well as the Set and<br />

HashSet classes. These <strong>Java</strong> “collection” classes will manage the List objects and Set object that we’ll use to track the<br />

current Actor objects that are involved in the game play on the screen. We’ll cover these java.util package classes in<br />

detail early on during this chapter, so get ready to learn about <strong>Java</strong> Array objects, and some other fairly advanced <strong>Java</strong><br />

programming concepts that may be a challenge for the beginner. However, they will be very useful for you to use in<br />

your <strong>Java</strong> 8 game title development work process, so I have decided to include them in this book.<br />

We will also want to create our first actor for the game, the InvinciBagel character actor, since I don’t want to get<br />

too far removed from the code that we wrote in Chapter 8 without implementing it (using it to create an actor). We will<br />

accomplish this by creating a Bagel.java class that will use the <strong>Java</strong> extends keyword to subclass a Hero.java abstract<br />

class. This makes Bagel.java into a subclass, and makes Hero.java a superclass.<br />

Once we have a Bagel.java class in place, we will then use the <strong>Java</strong> new keyword and the Bagel() constructor<br />

method for the Bagel class to create a Bagel object named iBagel. We will load the iBagel object with some temporary<br />

SVG data, at least until we get into how to create complex SVG collision shape data in Chapter 16 covering collision<br />

detection. We will also pass an X and Y coordinate, to put the iBagel actor in the middle of the screen, and finally the<br />

9-character movement sprite “cels,” which we first looked at during Chapter 8.<br />

We will do this so you can start to utilize the primary data fields (variables, properties, or attributes) that we<br />

installed in a public abstract Actor and Hero class infrastructure, which we so painstakingly (or, should I say, lovingly)<br />

designed back in Chapter 8.<br />

We will also be working in our InvinciBagel.java primary application class again here in Chapter 10, and will<br />

create our iBagel Bagel (Hero) object in a new .createGameActor() method we will be coding, so that we can wire our<br />

main character up to the GamePlayLoop class’s .handle() method. This will then access (call) the Bagel class .update()<br />

method, so that we can start to control the movements for the primary hero for our game, the InvinciBagel himself.<br />

www.it-ebooks.info<br />

207

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

Saved successfully!

Ooh no, something went wrong!