Store Objects in an Array
When storing the data used to draw three squares, it's okay to declare three
separate variables named squareA
, squareB
, and
squareC
, one for each square. But it becomes a problem if we need
to store the data to draw a hundred squares or if we don't know how many buildings
we'll be drawing ahead of time, as in the Animated Cityscape Challenge.
One way to store an arbitrary number of values in a variable is by using an array.
An array is basically a list with an index starting at 0. Imagine a list of books
stored in an array named bookArray
. We can access the first book in the
list at bookArray[0]
and the second book at bookArray[1]
.
To add a new book at the end of the list, we use the array's push()
method,
and to find the number of books in the list, we access the array's length
property.
In this example, we update the program from the previous example to store each
square's data in an array.
Instead of declaring the three separate variables squareA
,
squareB
, and squareC
, we start by declaring the global
variable squareArray
:
var counter;
var squareArray;
Inside the initScene()
method, we initialize squareArray
as an empty array, []
. We have to initialize squareArray
as an array before we can use the push()
method to add data to the end
of the array.
function initScene() {
counter = 0;
squareArray = []; // Initialize the variable as an empty array
squareArray.push( {x: 200, y: 40, s: 60} ); // Add the data for the first square to the end of the array
squareArray.push( {x: 80, y: 120, s: 40} ); // Add the data for the second square to the end of the array
squareArray.push( {x: 240, y: 220, s: 80} ); // Add the data for the third square to the end of the array
drawScene();
}
At this point, we have pushed the data for three squares onto the end of the array,
and we can access the data for the first square at squareArray[0]
, the
data for the second square at squareArray[1]
, and the data for the third
square at squareArray[2]
. By using an array, we can easily store the
data for a hundred squares in a single variable.
When working with objects and arrays, it's important to understand where values
are stored and how to access them. Keep this reference in mind when working with
the data stored in squareArray
:
squareArray // Used to access the array containing the data for all the squares
squareArray[0] // Used to access the data for the first square in the array
squareArray[0].x // Used to access the x-coordinate in the data for the first square in the array
Inside the drawScene()
function, we could draw each square
by passing its data into the drawSquare()
method manually like this:
function drawScene() {
drawBackground();
drawSquare(squareArray[0]);
drawSquare(squareArray[1]);
drawSquare(squareArray[2]);
}
But we are going to automate the process using a for loop. This way, if we add
the data for a fourth square to the array, the drawScene()
function
will automatically draw it, too.
function drawScene() {
drawBackground();
for (var i = 0; i < squareArray.length; i += 1) {
drawSquare(squareArray[i]);
}
}
Note that the for loop runs as long as i < squareArray.length
. This
is because, while the length of the array is 3, the last square's data is at index
2. Remember, the index of an array starts at 0, so the last item in an array is
always at index length - 1
.
Finally, we update the updateScene()
function to use
squareArray
instead of squareA
, squareB
,
and squareC
.
function updateScene() {
counter += 1;
squareArray[0].x -= 5;
squareArray[1].x += 5;
squareArray[2].y -= 5;
drawScene();
}
Click on the canvas to see the scene update. Change the values stored in the
objects to change the size and position of the squares and push another set of
data into the array to add another square. To learn more about arrays,
visit the
Arrays
lesson.
Quick Reference:
Coordinates
Variables
Functions
For Loops
Objects
Arrays
fillRect()
fillStyle