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 8 ■ Creating Your Actor Engine: Design the Characters for Your Game and Define Their Capabilities<br />

Notice that in addition to the .get() and .set() methods generated, for the boolean variables there is also an<br />

additional .is() method that is generated instead of the .get() method. Since I already named the boolean flags using<br />

the “is” preface, I am going to remove the second “Is” so that these “double is” methods are more readable. I am also<br />

going to do this to the hasValu method, so that inquiring as to the boolean setting in the method call is more natural,<br />

for instance, .hasValu(), isFlipV(), isBonus(), isFixed() or .isFlipH() for instance. I suggest that you perform the same<br />

edits with your code, for readability purposes.<br />

Now we are ready to create our Hero subclass, which will add another eleven attributes to the thirteen we have<br />

created in the Actor class, bringing the total to an even two dozen. These additional eleven attributes held in the<br />

Hero class will be used with movable sprites that can move around the screen (I like to call these motion sprites). Our<br />

InvinciBagel character will be the primary Hero Actor object in the single-player version of our game, and for a future<br />

multi-player version, this would include the InvinciBagel Hero Actor object and the Enemy Hero Actor object as well.<br />

Creating a Hero Superclass: Motion Actor Attributes<br />

Let’s create our public abstract Hero class next! This class will be the foundation for motion sprites that we will be<br />

creating for the game during this book. Create your Hero.java class in NetBeans, and declare it as public abstract<br />

class Hero extends Actor. Since we have done a lot of the “heavy lifting” in the Actor class, you will not have to<br />

create an ImageView to hold the sprite Image assets, or the List ArrayList object loaded with a List object<br />

filled with Image objects, or an SVGPath Shape object to hold the collision shape SVG polyline (or polygon) path data.<br />

Since we don’t have to declare any primary attributes, as those are inherited from the Actor superclass, the first<br />

thing we are going to do is to create a Hero() constructor method. This will contain your collision Shape data in a<br />

String object, the sprite X, Y location, and the Image objects that will be loaded into the List ArrayList object.<br />

After we create a basic Hero() constructor method, we will finish figuring out the other attributes (or variables) that<br />

your motion sprites will need to contain, just like we did when we designed the Actor superclass.<br />

Remember that you already have the spriteBound SVGPath Shape object, imageStates List ArrayList<br />

object, SpriteFrames Image object and iX and iY variables constructed in the Actor class using Actor() method.<br />

We will also need these to be in place in order to be able to code our Hero() constructor method. Since these are all<br />

already in place, due to the java extends keyword in the Hero class declaration, all we have to do is use the super()<br />

constructor method call and pass these variables from the Hero() constructor up to the Actor() constructor. This will<br />

automatically pass these variables up into the Hero class for our use, using the <strong>Java</strong> super keyword.<br />

Therefore, we have everything we need to be able to code our core Hero() constructor method, so let’s get into<br />

that now. The Hero() constructor will take in the same number of complex parameters as the Actor() constructor.<br />

These include your collision shape data, contained in the String object named SVGdata, an “initial placement” X and<br />

Y location for the sprite, and a comma separated list of the Image objects (cels or frames) for the sprite, which I named<br />

Image... spriteCels. This Image... designation, which needs to be at the end of your parameter list, because it is “open<br />

ended,” means that the parameter list will pass in one or more Image objects. Your code will look like the following:<br />

public void Hero(String SVGdata, double xLocation, double yLocation, Image... spriteCels) {<br />

super(SVGdata, xLocation, yLocation, spriteCels);<br />

}<br />

By using super() to pass your core constructor work up to the Actor superclass Actor() constructor method, your<br />

code that you wrote earlier, inside of the Actor() constructor, will create the spriteBound SVGPath Shape object using<br />

the <strong>Java</strong> new keyword and the SVGPath Shape subclass, and will uses a SVGPath class .setContent() method in order<br />

to load the SVGPath Shape object with your collision shape to be used with the sprite image states. The iX and iY<br />

initial locations are set, and the imageStates List array is loaded with sprite Image objects, passed in from<br />

the end of the parameter list.<br />

www.it-ebooks.info<br />

177

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

Saved successfully!

Ooh no, something went wrong!