Using the Modulus function
Author: | 醤油の兵士
|
Submitted: | 17th August, 2004
|
Views: | 5559
| Rated: |
|
|
Many people seem to not know how to use the Mod function or not know that it even exists. There weren't any articles about it so I decided to write one.
What does Modulus do?
The modulus function is a function that will return the remainder of a division, though it may not seem useful at first, there are many uses for it.
So where can I use it?
Examples of usages for modulus are:
1:Wrapping an object
2:Making a value wrap after a certain point (i.e. Like in many games you collect 100 blahs, you get a life)
3:When building a grid of a customizable width.
number one is now obsolete due to the release of the Frame Resize object by vortex.
number two is simple to do without modulus, but it can be done in one less action that way. You can just put the event:
On picking up coin:
Add 1 coin to counter
Set Counter to Counter Mod 100
this way it will automatically wrap itself back to 0, while not necessary, it does show you how the modulus function works
as for number 3, it can be used to calculate when to start the next row. like so:
calculating the column number:
the column number is equal to current item no. mod width
calculating the row number:
the row number is equal to Floor( current item no. / width )
these are 0 based indexes.
The actual Function:
The math behind the function X mod Y is
x - y * floor(x/y)
and that concludes my article on the Modulus function
|
|
醤油の兵士Almost Human Registered 12/06/2002
Points 173
|