Neil Webb is a Flash Platform Developer currently living and working in England. He joined the site when it was just a baby - look at it now! Neil has previously worked for Cambridge University, FIFA and Hutchinson Whampoa among others.Download the fla containing these examples (zipped)
Most people know how to use loops, but surprisingly few people realize the power of nested loops. This tutorial aims to give you a solid understanding of how they work and ideas for how they can be used in your Flash movies. If you don't know how to use a for loop then read Jesse's tutorial on Scripted Loops first. That will get you up to speed. This tutorial should be especially useful as a precursor to beginning XML, but then loops are useful for pretty much anything!
[as]//------nested loops-------
//outer loop
for (i=0; i<2; i++){
trace("outerloop");
//inner loop
for(j=0; j<2; j++){
trace("innerloop");
}
}
[/as]
[as]myLoop1();//call the first function
function myLoop1(){
for(i=0; i<2; i++){
trace("loop1");
myLoop2();//call the second loop from within the first loop
}
}
function myLoop2(){
for(j=0; j<2; j++){
trace("loop2");
}
}
[/as]
[as]myLoop1();//call the first function
function myLoop1(){
for(i=0; i<2; i++){
trace("loop1");
myLoop2();//call the second loop from within the first loop
trace ("finished an iteration of loop 1");
}
trace ("finished loop 1");
}
function myLoop2(){
for(j=0; j<2; j++){
trace("loop2");
}
trace ("finished an iteration of loop 2");
}
[/as]
[as]myLoop1();//call the first function
function myLoop1(){
for(i=0; i<2; i++){
trace("loop1");
myLoop2();//call the second loop from within the first loop
}
trace ("finished loop 1");
}
function myLoop2(){
for(j=0; j<2; j++){
trace("loop2");
myLoop3();//call the second loop from within the first loop
}
trace ("finished loop 2");
}
function myLoop3(){
for(p=0; p<2; p++){
trace("loop3");
}
trace ("finished loop 3");
}
[/as]
[as]myLoop1();//call the first function
function myLoop1(){
for(i=1; i<3; i++){
trace("person one passes a deck of cards to person two");
myLoop2();//call the second loop from within the first loop
}
trace ("finished deck");
}
function myLoop2(){
for(j=1; j<5; j++){
trace("__person two passes suit " + j +"/4 from deck " + i + " to person 3");
myLoop3();//call the second loop from within the first loop
}
trace ("_finished sorting suit " + (j-1) + " of deck " + i);
}
function myLoop3(){
for(p=1; p<13; p++){
trace("______person three sorts card number " + p + "/12 from deck " + i+ ", suit " + j );
}
trace ("_finished sorting cards from suit " + j);
}
[/as]


