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

These five variables or attributes hold the “core” properties for any sprite; the spriteFrame ImageView and the<br />

List ArrayList of Image assets (one to many visible states) that it holds (this defines what the sprite looks<br />

like), the spriteBound collision Shape area (defines what is deemed to have intersected with the sprite), and the X, Y<br />

location of the sprite on the display screen.<br />

These are also the five variables that will be configured using your Actor() constructor method, as well as your<br />

Hero() constructor method later on. First we will create the Actor() constructor; after that, we will add in all the other<br />

variables that we will need every Actor subclass to include.<br />

After we create all of the other variables for the Actor class, which are not set using the Actor() constructor method,<br />

we will initialize these to hold their default values inside of the constructor method, and finally we will have NetBeans<br />

create .get() and .set() methods for our variables using an automatic coding function which you’ll really like.<br />

The parameters that we will code to pass into this Actor() constructor will include the String object named<br />

SVGdata, which will contain a string of text defining the SVGPath collision shape, as well as the sprite X, Y location, and<br />

a comma delimited List of Image objects. The SVGPath class has a .setContent() method that can read or “parse” raw<br />

SVG data strings, and so we will use this to turn the String SVGdata variable into our SVGPath collision Shape object.<br />

We will not be implementing the collision code, or SVGPath Shape object during this chapter, or the next for<br />

that matter, but we need to put them in place, so we can use them later during Chapter 16 on collision detection<br />

processing and how to create collision polygon data using the GIMP and PhysEd (PhysicsEditor) software packages.<br />

The Actor constructor method that we will be creating will follow the following constructor method format:<br />

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

Later on if we need to create more complex Actor() constructor methods, we can “overload” this method by<br />

adding other more advanced parameters, such as pivot point pX and pY, for instance, or the isFlipH or isFlipV boolean<br />

values, to allow us to mirror fixed sprite imagery horizontally or vertically. Your <strong>Java</strong> code will look like the following:<br />

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

spriteBound = new SVGPath();<br />

spriteBound.setContent(SVGdata);<br />

spriteFrame = new ImageView(spriteCels[0]);<br />

imageStates.addAll(Arrays.asList(spriteCels));<br />

iX = xLocation;<br />

iY = yLocation;<br />

}<br />

Notice that the ImageView constructor, invoked using the <strong>Java</strong> new keyword, passes the first frame (Image) of<br />

the List ArrayList data you are passing in using a comma delimited list by using a spriteCels[0] annotation.<br />

If you were to create an overloaded method that allowed you to set up the pivot point data, it might look like this:<br />

public Actor(String SVG, double xLoc, double yLoc, double xPivot, double yPivot, Image... Cels){<br />

spriteBound = new SVGPath();<br />

spriteBound.setContent(SVG);<br />

spriteFrame = new ImageView(Cels[0]);<br />

imageStates.addAll(Arrays.asList(Cels));<br />

iX = xLoc;<br />

iY = yLoc;<br />

pX = xPivot;<br />

pY = yPivot;<br />

}<br />

As you can see in Figure 8-5, you will need to use the Alt-Enter work process, and have NetBeans code your<br />

import statements for the Arrays class for you. Once you do this, your code will be error free.<br />

www.it-ebooks.info<br />

169

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

Saved successfully!

Ooh no, something went wrong!