3D Game Programming

(C. Jardin) #1
then tell the Tween that we want to rotate to the new Y rotation passed to the
spinAvatar() function. Every time the animation runs, the stuff inside onUpdate()
is what happens. The rotation of the avatar’s head is updated to the Y rotation
of the Tween. The last line starts it all.

The periods at the end of each line in that function represent method chaining.
In JavaScript, a semicolon ends a “sentence” of code. The period in JavaScript,
unlike in English, indicates that we want to do something else with the current
code—that the code sentence is not over yet.

We could have put all of that code on a single line, but it can be easier for
humans to read code split across lines (computers don’t care). Method
chaining works with only certain JavaScript objects, like Tweens. It is a
somewhat common practice in JavaScript, though we won’t be using it much
ourselves.

Try This Yourself
We told the Tween library that it will run from start to finish in 100
milliseconds. This number was at the end of the line that started
with to.

It would take 1000 milliseconds to make one second, so 100 mil-
liseconds is less than a second. The spin of the avatar takes less
than a second. Experiment with that number to get it the way that
you like. Is 1000 too long? Is 10 too short? You decide!

11.6 The Code So Far


If you would like to double-check the code in this chapter, turn to Section
A1.8, Code: Turning Our Avatar, on page 226.

8.6 What’s Next


Wow! Our simple avatar simulation is getting quite sophisticated, isn’t it?
We’ve already put quite a bit of work into our avatar, but you may have noticed
that it can pass right through our trees. In the next project chapter we’ll talk
about collision detection, and use it to make our avatar stop when it collides
with a tree.

But first it’s time to take a closer look at all of that JavaScript code that was
added for us when we started this project.

Chapter 8. Project: Turning Our Avatar • 84


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf