This swf illustrates a basic scenario of outputting various elements in an Array in a continuous loop.
Let’s not waste any time. I’m going to show you how to take all this
function nextDay(e:MouseEvent):void
{
if (count < numDays - 1)
{
count++;
}
else
{
count = 0;
}
day_txt.text = days_array[count];
}
And nuke it down to just this:
function nextDay(e:MouseEvent):void
{
day_txt.text = days_array[ ++count % numDays ]
}
interested?
Watch this for 3 minutes at least
*note I apologize that my function is named toggleWindow instead of nextDay in the video. I think youtube was hacked:)
What the heck does % do?
Did you watch the video? Ok, fine. The modulo operator which looks like % does a nifty little trick for us. It takes the number on the left and divides it by the number on the right. When the operation is done, it gives us the REMAINDER.
Consider the following expression
11 % 5 will return 1
why? Because 11 divided by 5 gives us 2 with a remainder of 1.
Still not clear?
5 x 2 = 10
11-10 = 1
Check out the following traces from my modulo demo swf (as seen in the video)
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
7 % 5 = 2
8 % 5 = 3
9 % 5 = 4
10 % 5 = 0
11 % 5 = 1
12 % 5 = 2
13 % 5 = 3
14 % 5 = 4
15 % 5 = 0
16 % 5 = 1
17 % 5 = 2
18 % 5 = 3
19 % 5 = 4
20 % 5 = 0
21 % 5 = 1
22 % 5 = 2
23 % 5 = 3
24 % 5 = 4
The main take away here is that any value on the left can be converted to a number from 0 to 4. So as our counter goes up, we can always convert it down and force it into our range. This comes in real handy when looping through Arrays, Objects Strings, XMLLists or anything else that is indexed.
Let’s say we clicked our button 22 times and our array has 5 items in it.
Well, 5 goes into 22 4 times.
4 x 5 = 20
22-20 gives us a remainder of 2.
Well 22 clicks would have us sucking the item with an index of 2 out of our array.
I’m sure someone else has already done this, but from this point forward it is perfectly acceptable and expected that all members of the snorklsphere refer to this technique as the schooff loop. thanks.
Download Source Files
Notes
1: As Andreas points out in the comments, this will not work going backwards:
The only problem is going backwards, since negative numbers won’t be positive when modulo is applied to them:
trace(-1 % 7); // -1
trace(-2 % 7); // -2
// …
trace(-6 % 7); // -6
trace(-7 % 7); // 0
trace(-8 % 7); // -1
You would still need an “if” in order to make sure numbers aren’t negative and add 7 if they are..
2: As Harry mentioned below, at some point I mis-counted. The full length of the array is 7 so numDays will start at 7 when the days_arr is full.
3: If you want to do a simple toggle switch for play/pause or on/off check out the Conditional-Less Boolean Toggle AKA Switcheroo
thanks guys for your help.
wrap up
Have fun with this one. I’m going easy on you as massive LoaderMax stuff and a new AS3 code challenge are coming soon!
If you found this tip helpful feel free to share it on your social network of choice.
Best,
Carl






