3D Game Programming

(C. Jardin) #1
To resolve this problem, let’s tell our JavaScript program how to eat food. We
do this by adding a function that explains eating after the line with eat(food).

food ='Cookie';
eat(food);

functioneat(food) {
console.log(food +"! Nom. Nom. Nom.");
}

At this point, there should be no compile-time errors in ICE, no run-time
errors in the JavaScript console, and the message, “Cookie! Nom. Nom. Nom.”
in the console.

Before we wrap up this chapter, let’s look at some common 3D-programming
errors. Add the following code after the closing curly brace of the eat() function:

varshape =newTHREE.SpherGeometry(100);
varcover =newThree.MeshNormalMaterial();
varball =newTHREE.Mesh(shape, cover);
scene.ad(ball);

You’ll notice that there are no compile-time errors in ICE for this code. The
browser reads the JavaScript code and says, “Yup, that looks like perfectly
fine JavaScript to me. I’ll run it now!” However, problems pop up when the
code is actually run, and you’ll see run-time errors in the JavaScript console.

Possible Error Message—Undefined Is Not a Function


Let’s take a look at what went wrong. First, open the JavaScript console if
it’s not already open. In there, you should see a very unhelpful message.

This message is trying to tell us that SphereGeometry is spelled incorrectly. Check
the code; it turns out we missed an e and typed SpherGeometry instead. The
message in the JavaScript console is very poor and unhelpful.

There are two problems to tackle here. First, “undefined is not a function”
doesn’t really tell us anything and is not easy to understand. Even JavaScript
experts get confused by that one.

The second problem is the line number in the error message. In this example,
gamingjs.com:25 means the browser thinks the problem happened on line 25 of

report erratum • discuss

Debugging in the Console • 21


Prepared exclusively for Michael Powell

Free download pdf