WEEK: 13
Active: November 18th - November 24th
Work Due: November 25th @ 11:59PM

Why Learn about Arrays?

We have been creating items on the screen, and they have been moving, and that has been fantastic. However, we created only two items along with the player.

What happens when I have to add another? Let’s look at some code.

    var x = 50;
    var y = 50;
    var diameter = 25;

Do you remember this? We needed these three variables to create one circle. We created a function with random numbers so that we could create multiple circle objects. That was pretty cool. However, they were all created in the draw. Which is fine, but remember, it made them bounce all around. What if we wanted to create multiple circles that did not move?

At the moment, we would have to do something like this.

    var x = 50;
    var y = 50;
    var diameter = 25;
    var x1 = 150;
    var y1 = 150;
    var diameter1 = 125;

    function setup()
    {
        createCanvas(800,600);
    }

    function draw()
    {
        circle(x,y,diameter);
        circle(x1,y1,diameter1)
    }

What if you wanted a third? A fourth? What if I wanted 500 circles? Yikes! Do you see what is happening here?

We have arrays, so we don’t have so many variables. With arrays, we can add as many circles as we want and then do something with them.
Click here for this section’s Video


Previous section:
Next section: