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

It’s important to note that since we’re setting this up in this way that the Hero class has access to everything<br />

that the Actor class has (thirteen powerful attributes). Actually, it may be more “salient” to look at this the other<br />

way around, that the Actor (fixed sprites) class has every capability that the Hero (motion sprites) class. This<br />

power should be leveraged for level design wow factor, including multi-image states (List Array), custom<br />

SVGPath collision shape capability, custom pivot point placements, and the ability to flip (mirror) sprite imagery<br />

around either the X axis (FlipV = true) or the Y axis (FlipH = true) or both axes (FlipH = FlipV = true). Putting these<br />

capabilities into place in your Actor Engine (Actor and Hero abstract superclasses) is only the first step; using them<br />

brilliantly for your game’s design and programming, as time goes on and you continue to build and refine the game,<br />

is the ultimate goal for putting this foundation into place during this chapter. As you can see in Figure 8-10 our basic<br />

(core) constructor code is error free.<br />

Figure 8-10. Create a public abstract class Hero extends Actor and add a constructor method and a super() constructor<br />

Adding Update and Collision Methods: .update() and .collide()<br />

Now that we have a basic constructor method, which we’ll be adding to a bit later, let’s add the required abstract<br />

.update() method, as well as a .collide() method, as motion sprites are moving, and therefore can collide with things!<br />

First let’s add in the public abstract void .update(); method, as it is required by our Actor superclass. Doing this<br />

essentially passes down (or up, if you prefer) the implementation requirement for this .update() method, from Actor<br />

superclass to Hero subclass, and on to any future subclasses of Hero (which will make Hero into a superclass, and<br />

more reflective of its name). Future non-abstract (functioning) classes will implement this .update() method, which<br />

will be utilized to do all the heavy lifting for the game programming logic. As you can see in Figure 8-11, motion sprites<br />

(Hero subclasses) will also need to have a collision detection method, which I will call .collide(), as that is a shorter<br />

name, and that, at least for now, will remain unimplemented except for returning a boolean false (no collision here,<br />

Boss!) boolean data value. Your <strong>Java</strong> code for the .collide() method structure will take an Actor object as its parameter,<br />

since that is what your Hero object will be colliding with, and should look like the following:<br />

public boolean collide(Actor object) {<br />

return false;<br />

}<br />

178

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

Saved successfully!

Ooh no, something went wrong!