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

public void setGravity(float gravity) {<br />

this.gravity = gravity;<br />

}<br />

public float getBounce() {<br />

return bounce;<br />

}<br />

public void setBounce(float bounce) {<br />

this.bounce = bounce;<br />

}<br />

It is important to note that objects created using the Hero class also have access to the getter and setter methods<br />

that we generated earlier for the Actor class. In case you are wondering what the <strong>Java</strong> keyword this means in all<br />

of these .set() methods, it is referring to the current object that has been created using the Actor or Hero class<br />

constructor method. Thus if you call the .setBounce() method off of the iBagel Bagel object (which we will soon be<br />

creating in Chapter 10), this keyword is referring to this (iBagel) Bagel object instance. So if we wanted to set a 50%<br />

bounce factor, we would make the following method call using our new .setBounce() setter method:<br />

iBagel.setBounce(0.50);<br />

Next let’s take a look at how these sprite Actor classes fit in with the other classes we have coded during the book<br />

so far. After that, we will summarize what we have learned so far during this chapter, and we can move on into the<br />

future chapters in this book, and use these classes to create sprites for our game, as we learn how to use sprites for<br />

game play.<br />

Updating the Game Design: How Actor or Hero Fit In<br />

Let’s update the diagram that I introduced in Chapter 7 (Figure 7-19) to include the Actor.java and Hero.java classes.<br />

As you can see in Figure 8-16, I had to switch the .update() physics and .collide() collision parts of the diagram, as<br />

the Actor class only includes the .update() method, and the Hero class includes both of these methods. Since the<br />

.collide() method will be called in the .update() method, I connected these two parts of the diagram with a chrome<br />

sphere as well.<br />

Figure 8-16. The current invincibagel package class (object) hierarchy, now that we have added Actor and Hero classes<br />

www.it-ebooks.info<br />

185

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

Saved successfully!

Ooh no, something went wrong!