Using Modulo Part 2
I posted a while ago about using modulo and I've recently used it on a couple of things. What I like about it is that it resets the value of a counter if you're using one, like so:
Actionscript:
-
var bg:Number = 0;
-
var total:Number = 5;
-
-
function addNum():Void
-
{
-
trace(bg);
-
bg++;
-
bg %= total;
-
}
-
-
for (var i:Number = 0; i <50; i++) {
-
addNum();
-
}
What this does is outputs 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, etc. 10 times (because it is running the loop 50 times and each time it outputs 0-4, it resets and does it again). You can see this would be handy if you have a variable that you need to reset every few iterations and is something I've used quite often lately. It definitely beats the hell out of using an if/else statement to reset it to 0.