Upgrade Your Account!

Hey! To start this decade right, we believe Newgrounds should be ad-free for EVERYONE. For this to work, we need people who have the means to become paying supporters. For just $3 per month or $25 for the year, you can help this happen and get some nice perks!

For the month of January we have disabled ads on all E-M content. We need 170 people to reach our January goal of 3,000 active supporters. If we hit that, we'll continue with a new goal in February!

Please check out our Supporter Upgrade and consider coming aboard!

AS: Modulo 2006-08-14 05:27:30

AS: Modulo

as%main = 0

Time to write a short post on the Modulo assignment. Reason being, most of the assignment operators in Flash (such as += or -=) are pretty hardwired into our brains; although some just take what we know and twist it completely (like the Bitwise Operators). Modulo is one of these operators that people don't actually know about unless they've been taught it. Besides - this seems like an easy enough subject that I can use as the topic of my first tutorial that no one has written anything about

Well, Bar T-H in Math-Simple, but that doesn't count

Who will Benefit from this Guide?
Everyone working on a tile based game, dress up sim, top down shooter, or wants thier numbers to look exact without needing to use whole numbers.

Given two numbers, a and b, a modulo b (written in Flash and most other C based languages as a%b) is the remainder of a division of a by n. It's taking you back to fourth year mathmatics where you had to find that 15 divided by 4 was 3, with three left over, back before long division dominated our way of thinking.

For example:
9 % 2.0 = 1 (9 divided by 2 is four, remainder 1)
9 % 5.0 = 4 (9 divided by 4 is one, remainder 4)
9 % 3.0 = 0 (9 divided by 3 is three, exactly)
9 % 1.2 = 0.6 (9 divided by 1.2 is seven, remainder 0.6)

Simple Modulo Code:
var x:Number = 10;
var y:Number = 3;

var div:Number = Math.floor(x/y);
var mod:Number = (x%y);

trace(x+" / "+y+" = "+div+". Remainder "+mod);
// Outputs: "10 / 3 = 3. Remainder 1"
// Try changing the value of X and Y to other values to demonstrate the modulo functions

Apart from teaching Key Stage 1 maths then, what good is the modulo? Think about the maths for a second: x - x%y will always return a multiple of y. This can snap your variables to any value you want

Example of Always Returning a Multiple:
var snap:Number = 20;
//All our answers will end up being multiples of this number! Try changing it and testing it out...
var w:Number = 20;
var x:Number = 19;
var y:Number = 21;
var z:Number = 210;
var a:Number = 573882.673628;

Example Uses:
- Anywhere you need to know how much of something is left over after it has been shared out can benefit directly from a simple a%b function. Maybe you want to tell the player of your next top-down-shooter exactly how much ammo clips they have remaining, and just exactly how much spare ammo they have left?

- Sham Bhangal demonstrates a snapping method in his Flash Hacks book (Hack #84, available here in Flash 7 format in Chapter 10, 84_dragger). With it, he's made tiles that snap instantly into place - a perfect starting block for anyone making a jigsaw puzzle, dress up sims (because not having clothes snap onto people makes the thing look very sloppy), Bubble Bobble or Mean Machine style puzzle games, tile based level editors, or anything with a drag and drop user interface.

- Personally, I'd use it to make sure the user always sees values I want them to be able to understand - especially if the underlying numbers would make it especially complicated. No one needs to see that thier RPG character has 2056.7882222 EXP points if 2056.75 would do the job for the user interface.

- Or it could be used in preloaders where you DO want to display the decimal point (doesn't it just make it feel the flash is loading that extra bit faster if the preloader number changes a thousand times instead of a hundred? ^_^), but only have it snap to every 0.1% so that your numbers don't look like they're being spewed out randomly.

Finally, everyone's free from needing to trim everything down to integers using Math.floor() and Math.ceil() and are free to snap thier values to whatever they want.


BBS Signature

Response to AS: Modulo 2006-08-14 05:45:34

That was well confusing.

Response to AS: Modulo 2006-08-14 05:51:08

This is pretty useful for people who don't understand the weirdo modulo function, or can't think of any uses for it.

As far as I could tell by skim reading it, though, you didn't really cover the assignment; rather the operator. A modulo assignment is good for a short way of making simple resets and stuff. For example:

i ++;
i %= 500;

Whilst an if(); statement would take at least another two lines, which you really could do without if you're writing a big block of code.

There are also lots of other uses but none spring to mind right now.

BBS Signature

Response to AS: Modulo 2006-08-14 05:58:13

At 8/14/06 05:51 AM, _Paranoia_ wrote: As far as I could tell by skim reading it, though, you didn't really cover the assignment; rather the operator.

Yeah. My mistake...
Still. Hopefully gave some food for thought and did a good first AS tute.


BBS Signature