Using Functions

Now that we understand what r() does, let’s put it to use. Back in the makePlanet() function, change the values of x, y, and z as shown.

​ ​function​ makePlanet() {
​  ​var​ size = 50;
» ​var​ x = r(1000) - 500;
» ​var​ y = r(1000) - 500;
» ​var​ z = r(1000) - 1000;
​  ​var​ surface = ​'purple'​;
​ 
​  ​var​ shape = ​new​ THREE.SphereGeometry(size);
​  ​var​ cover = ​new​ THREE.MeshBasicMaterial({color: surface});
​  ​var​ planet = ​new​ THREE.Mesh(shape, cover);
​  planet.position.​set​(x, y, z);
​  scene.add(planet);
​ }

Calling r(1000) will give us a random number between 0 and 1000. For x, we subtract 500 from r(1000) to give us a random number ...

Get 3D Game Programming for Kids, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.