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 ...

Get Unity 4.x Game Development by Example Beginner's Guide 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.