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 10 ■ Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class<br />

Figure 10-15. The Hero abstract class has a public boolean collide() method but since it is not abstract it is not required<br />

You might be wondering why we didn’t make .collide() into an abstract method, which it’s important to note that<br />

we could do, at any point in time in the future. The reason is that we might want to have (add in) motion sprites that<br />

do not collide with anything in the scene (game play) at some point in your future game development, perhaps to add<br />

in visual detail elements, such as a bird flying across the top of the screen. The choice is yours to make, so if you want<br />

motion sprites to always collide with things, you could declare the .collide() method to be abstract as well.<br />

The important thing to note is that we can still override the .collide() method, which I am going to do next,<br />

just to show you that this can still be done without the method having to be declared using the <strong>Java</strong> abstract keyword<br />

and the Bagel class will use the overridden .collide() method rather than the “default” method in the Hero superclass,<br />

which returns a false value (no collision).<br />

What is important to make a note of here is that you can put your default method code into the superclass,<br />

which, if not specifically overridden in any given subclass, will become your default method programming logic for all<br />

of your subclasses. This allows you to implement “default” behaviors for all your subclasses, in one place (superclass).<br />

Of course, you can always override this default behavior, and implement a more specific behavior, using the<br />

@Override keyword along with the same exact method declaration format. This can be seen in Figure 10-16, near the<br />

bottom of the screen shot, and if you compare this with the bottom of Figure 10-15, you will see that their structure is<br />

identical, with the exception of the @Override keyword used in the Bagel subclass. When we cover collision detection<br />

programming, we’ll replace the return false; line of code with our Bagel class’s own customized .collide() collision<br />

detection behavior, which will become quite complex as we add advanced features to the game as time goes on. For<br />

now I am installing this .collide() method body (essentially empty, as it just returns false) so you see a complete class.<br />

226<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!