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-5. Create a constructor method to set up fixed Actor sprite subclasses with collision shape, Image list, location<br />

Next, let’s code the other crucial method for this class, the abstract .update() method, and then we can add the<br />

rest of the Actor class fixed sprite attributes that we will need. After that, we can initialize the additional variables<br />

inside of the Actor() constructor method. Finally we will learn how to create “getter and setter” methods for the Actor<br />

class, before we move on to use this new custom Actor superclass to create our other Hero motion sprites subclass.<br />

Creating an .update() Method: Connect to GamePlayLoop Engine<br />

The most important method for any sprite class, other than the constructor method that births it into existence, is the<br />

.update() method. The .update() method will contain the <strong>Java</strong> 8 code that tells the sprite what to do on every pulse<br />

of the GamePlayLoop. For this reason, this .update() method will serve to “wire” the Actor sprite subclasses created<br />

using our Actor superclass and Hero subclass into the GamePlayLoop timing engine that we created in Chapter 7.<br />

Because we need to have an .update() method as part of every Actor object (actor sprite) in our game, we’ll need<br />

to include an “empty” (for now) abstract .update() method in the Actor superclass we are coding currently.<br />

As you learned in Chapter 3, this public abstract method is left empty, or more accurately, unimplemented,<br />

in the Actor superclass, but will need to be (that is, will be required to be) implemented (or alternatively, to again<br />

be declared as an abstract method) in any Actor subclass, including the Hero subclass we are going to be coding<br />

later on.<br />

The method is declared public abstract void, as it does not return any values (it is simply executed on each<br />

<strong>Java</strong>FX pulse event) and does not include the {...} curly braces, as it does not (yet) have any body of code inside of it!<br />

The single line of code that declares the public abstract (empty or unimplemented) method should look like this:<br />

public abstract void update();<br />

As you can see in Figure 8-6, the method is very simple to implement, and once you add this new method under<br />

your Actor() constructor method, your <strong>Java</strong> 8 code is again error free, and you will be ready to add more code.<br />

170<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!