Game Development Reference
In-Depth Information
Be sure not to call a invinciBa-
gel.scoreText.setText(String.valueOf(invinciBagel.gameScore));
statement, which updates the scoreText UI element using a .setText() method, inside of
each of these conditional if() statements. Instead, notice that I am placing it at the end
of the method, after all of these evaluations are completed. I am doing this so I only
have to call this one line of code one time, at the end of the conditional if() object pro-
cessing structure. That said, the Java code will still work if you place this scoreText ob-
ject score update statement inside each of the conditional if() statement code body
structures. However, I'm trying to show you how to write code that does a lot of work
by using relative few (a dozen or less) lines of code to accomplish major Java 8 game
development tasks.
Next, we are going to take the .playiSound0() method call out of the .checkColli-
sion() method, and we'll put it inside of the scoringEngine() method instead. As long
as it is in one of these two methods, it's going to get called on collision, one way or the
other. The reason that I am going to do it this way is because we will probably want to
play a different sound effect when a player finds a treasure, or is hit by a projectile. In
this way, your audio is associated with scoring and game play when different types of
collisions are detected, rather than just playing audio for any collision.
Our conditional if() structure code bodies use curly braces, which allows us to add
Java statements into each type of collision (scoring) object instance (type). So in addi-
tion to incrementing (or decrementing, as we will add later) the score, we can also use
different sounds (audioClips) for different objects. Let's add the .playSound() method
calls into these conditional if() code blocks next, so that we have the code in place to
trigger sound effects when a treasure is picked up by the primary character or when he
is hit by (or catches) a projectile shot by an enemy (an InvinciBeagle) character, or
when he collides with a prop in the scene.
This is done by using the following Java conditional if() structures, which can also
be seen in Figure 17-10 :
private void scoringEngine (Actor object ) {
if( object instanceof Prop ) {
invinciBagel. gameScore +=5;
invinciBagel. playiSound0 ();
}
if(object instanceof PropV ) {
invinciBagel. gameScore +=4;
invinciBagel. playiSound1 ();
}
 
Search WWH ::




Custom Search