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

Figure 8-6. Add an Arrays import statement to support constructor method; add a public abstract .update() method<br />

Next, we will add the rest of the attributes (or variables) for our fixed sprite Actor superclass, which requires us to<br />

think ahead, regarding what we want to be able to accomplish with our sprites during the creation of this game.<br />

Adding Sprite Control and Definition Variables to an Actor Class<br />

The next part of the process is easy, from a coding standpoint, as we will be declaring some more variables at the top<br />

of the Actor class. From a design standpoint, this is more difficult, however, as it requires that we think ahead as far as<br />

possible, and speculate about what variable data we will need for our sprite actors, both fixed and motion sprites, to<br />

be able to do everything that we want to during the construction of this game, as well as during its game play.<br />

The first additional variables I am going to declare after the iX and iY variables are the pX and pY pivot point<br />

variables. I had originally placed these in the Hero subclass, which we’re going to create next, once we are done with<br />

the creation of this Actor superclass. The reason I moved these “up” to the Actor superclass level is because I wanted<br />

to have the flexibility of rotating fixed sprites (treasure and obstacles) as well as motion sprites. This gives me more<br />

power and flexibility where level and scene design purposes are concerned. These pivot point X and Y variables would<br />

be declared as protected double data variables, and would be done using the following two lines of <strong>Java</strong> code:<br />

protected double pX;<br />

protected double pY;<br />

Next, we need to add some boolean “flags” to our Actor class (object) definition. These will indicate certain<br />

things about the sprite object in question, such as if it is Alive (for fixed sprites this will always be false) or Dead, or<br />

if it is Fixed (for fixed sprites this will always be true, and true for motion sprites that are not in motion) or Moving,<br />

or Bonus objects, indicating additional points (or lifespan) for their capture (collision), or Valuable, indicating<br />

additional powers (or lifespan) for their acquisition (collision). Finally, I’m defining a Flip Horizontal and Flip<br />

Vertical flag, to give me four times the flexibility with (fixed or motion) sprite image assets than I would have without<br />

these flags in place.<br />

www.it-ebooks.info<br />

171

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

Saved successfully!

Ooh no, something went wrong!