At the top of the code, you’ve started a code outline, which is good:
var gameOver = false;
addPlanets();
In that code outline, you’ve started the gameOver
variable as false
, which makes sense since the game probably hasn’t started at this point. Next you start adding planets in the addPlanet()
function.
In that function, you create 100 planets using the createPlanet()
function:
function addPlanets() {
if (gameOver) return;
for (var i=0; i<100; i++) {
createPlanet();
}
}
And in the createPlanet()
function, you set the size to a random number and then… you’ve got a mistake. You add that size to the planets
variable with push
:
function createPlanet() {
var size = r(50);
planets.push(size);
// ...
}
There are two problems here:
- You probably want to add planets to the list of items in the
planets
variable – not the size of the planets
- You haven’t defined what
planets
is.
To fix #2, go back to the code outline at the top and define planets
as an empty list:
var gameOver = false;
var planets = [];
addPlanets();
I think you already know how to fix #1 because you already have the right code for this later in your createPlanet()
function – it’s just commented out right now. So remove the line that currently does planets.push(size)
and un-comment the other two lines at the bottom of the createPlanet()
function.
Hopefully that all makes sense. If you have any other questions / problems, please ask
-Chris