3D Game Programming

(C. Jardin) #1
// You don't need to type this in:
varhead_shape =newTHREE.SphereGeometry(100);

The var keyword declares new things in JavaScript. It tells both the computer
and humans reading the code, “Get ready—something new is coming!”

There are lots of different kinds of things in JavaScript. In the little bit of code
you just saw, we’re making a new 3D sphere shape. Things can also be
numbers:

varmy_height = 1.5;

They can be words:


vartitle ="3D Game Programming for Kids";

Programmers usually call the things inside quotation marks strings. Here the
title item is a string that holds the title of this book.

Strings Are Easy to Break

Always be sure to close your quotes. If you forget, you’ll get very
weird errors that are hard to fix.

They can be true things:


varam_i_cool = true;
varam_i_dumb = false;

They can even be weird JavaScript things that mean nothing:


vari_mean_nothing = null;
vari_also_mean_nothing = undefined;

What Are These null and undefined Things?
You generally don’t have to worry about undefined or null things. It
doesn’t make much sense to create such things. If you see them
at all, it will be in a function—usually indicating that nothing was
found or created by the function.

Why var?


The var keyword is short for variable. A variable is a thing that can change:


vargame ="started";
// do some work here
game ="over";

A1.7 Code: A Closer Look at JavaScript Fundamentals


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf