Game Development Reference
In-Depth Information
Figure 10-15 . The Hero abstract class has a public boolean collide() method but since it is not abstract it is not re-
quired
You might be wondering why we didn't make .collide() into an abstract method,
which it's important to note that 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 do not collide with
anything in the scene (game play) at some point in your future game development, per-
haps to add 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 motion sprites to always collide with
things, you could declare the .collide() method to be abstract as well.
The important thing to note is that we can still override the .collide() method ,
which I am going to do next, just to show you that this can still be done without the
method having to be declared using the Java abstract keyword and the Bagel class will
use the overridden .collide() method rather than the “default” method in the Hero su-
perclass, which returns a false value (no collision).
What is important to make a note of here is that you can put your default method
code into the superclass, which, if not specifically overridden in any given subclass,
will become your default method programming logic for all of your subclasses. This al-
lows you to implement “default” behaviors for all your subclasses, in one place (super-
class).
Of course, you can always override this default behavior, and implement a more
specific behavior, using the @Override keyword along with the same exact method de-
claration format. This can be seen in Figure 10-16 , near the bottom of the screen shot,
and if you compare this with the bottom of Figure 10-15 , you will see that their struc-
ture is identical, with the exception of the @Override keyword used in the Bagel sub-
class. When we cover collision detection programming, we'll replace the return
false; line of code with our Bagel class's own customized .collide() collision detec-
tion behavior, which will become quite complex as we add advanced features to the
game as time goes on. For now I am installing this .collide() method body (essentially
empty, as it just returns false) so you see a complete class.
 
Search WWH ::




Custom Search