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 17 ■ Enhancing Game Play: Creating a Scoring Engine, Adding Treasure and an Enemy Auto-Attack Engine<br />

You will need to remember to add the Treasure objects to the castDirector object, as seen in Figure 17-17,<br />

because the collision detection engine uses this CastingDirector class (object) to manage a collision detection<br />

process. If you do not do this step, the InvinciBagel will go right over the Treasure objects without seeing<br />

(colliding with) them!<br />

Figure 17-17. Add iTR0 and iTR1 Treasure objects inside the .addCurrentCast(Actor. . .) method call<br />

Now that we have added these two Treasure objects to the castDirector CastingDirector object, the collision<br />

detection programming logic will “see” them, and collisions with the Treasure objects will occur, triggering the scoring<br />

engine to score the game correctly. Test the final code for implementing Treasure in your game, and make sure that it<br />

works, and then we can move on to adding adversaries and have them shoot projectiles at the InvinciBagel character.<br />

Adding Enemies: The Enemy and Projectile Classes<br />

Now that we have added positive (Treasure) elements to our game, let’s add some negative (Enemy and Projectile)<br />

elements to the game, so that we remain “balanced” in our development work process. We are going to use the Actor<br />

superclass, rather than the Hero superclass, to create the Enemy and Projectile classes (objects). This is because by<br />

doing it this way, we have a more optimized game, as we are only using one single .collide() method (remember that<br />

each Hero object has a .collide() method implemented) for our <strong>Java</strong>FX pulse event engine to process. When I convert<br />

this game into a multi-player game (the code for this is beyond the Beginner nature of this title), I would want to<br />

make the Enemy class a Hero subclass, so that the Enemy character could collide with things, such as Treasure and<br />

Projectiles, just like the InvinciBagel can. Since the Enemy class still has an .update() method, it can be moved around<br />

on the screen, where it can (and will) come out of hiding and shoot bullets (negative effect) and balls of cream cheese<br />

(positive effect) at the InvinciBagel character. The only thing that differentiates the Hero class is the .collide() method,<br />

www.it-ebooks.info<br />

407

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

Saved successfully!

Ooh no, something went wrong!