3D Game Programming

(C. Jardin) #1

11.5 Making Our Games Even Better


We’ve spent a good deal of time in this book adding animations to our avatar.
We do this partly to understand important concepts like grouping objects,
but also because this is a lot of what 3D-game programmers do.

Our avatar doesn’t really need to have hands and feet that move as in real
life, but this animation helps make the game seem more real. In this example,
the gameplay is pretty simple: press the space bar near the treasure to get
points.

What makes the game compelling and fun enough that players keep coming
back is a combination of interesting gameplay and the occasional glimpses
of realism.

Adding Animation and Sound


How many tweaks you add is up to you, the game programmer. But for this
chapter let’s add two together: we see an animation and hear a sound when
the avatar gets the treasure-fruit. Adding sound to the game is the easier of
the two, so we’ll tackle that first.

First we add Sounds.bubble.play() to the scorePoints() function:


functionscorePoints() {
if(scoreboard.getTimeRemaining() === 0)return;
scoreboard.addPoints(10);
Sounds.bubble.play();
}

You can find more information on the Sounds.js library in Section A2.5,
Sounds.js, on page 277. The library has a fairly small number of sounds to
pick from, but there should be enough to get started writing games.

With that line added, we can score points and hear sound when the avatar
jumps up to grab treasure-fruit. But we’re not actually getting any of that
golden fruit out of the tree.

To animate the fruit, we need to add the fruit to the avatar’s frame of reference,
then Tween it. The Tween will be a little different than those we have done so
far, as it will animate two things. It will rise above the avatar and it will spin.
The following code, which we can add after the scorePoints() function, will do
all of that:

varfruit;
functionanimateFruit() {
if(fruit)return;

report erratum • discuss

Making Our Games Even Better • 105


Prepared exclusively for Michael Powell

Free download pdf