Going loopy
The piece of code that follows is a touch trickier. We want to create 16 new cards and put them into our deck, the aGrid
array. To do that, we're using an "iterative loop" to populate our two-dimensional array.
An iterative loop is a piece of code that repeats itself, with special instructions telling it when to start and when to end. If a loop never ends, our game crashes, and we're flooded with angry tech support calls.
This line begins an iterative loop:
for(var i:int=0; i<rows; i++)
The variable i
is called the iterator. That's what we use to figure out how many times to loop, and when to stop. You don't have to use the letter i
, but you'll see it a lot in other people's code. It's a best practice. And, as we'll see shortly, when you start putting loops inside other loops, you'll actually have to start using other letters, or else your code will break.