3D Game Programming

(C. Jardin) #1

Figure 3—Selecting Make a Copy


Figure 4—Naming the Project


4.2 Building Interactive Systems with Keyboard Events


So far in this book, our code has been very linear—it follows a straight line.
First we made a cover, a shape, and a mesh, and then we added the objects
to the scene. Then we moved to the next mesh, which was also added to the
scene. Although it is possible to write a lot of JavaScript that looks like this,
most JavaScript programs tend to be quite different.

That’s because JavaScript usually runs in a web browser. In a web browser,
JavaScript code has to respond to events. A key being pressed on the keyboard,
a mouse button being clicked, and the mouse pointer moving around the
page are all events in the web browser. A crazy number of events can poten-
tially happen on every web page, and for the most part, these events are
ignored.

But we’re not going to ignore key presses. We’ll listen for events with something
called—you guessed it—an event listener. Let’s add the following at the very
bottom of our code, below the animate() line that we added in Chapter 3, Project:
Making an Avatar, on page 25.

Chapter 4. Project: Moving Avatars • 36


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf