forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloopIndex.as
More file actions
28 lines (24 loc) · 816 Bytes
/
loopIndex.as
File metadata and controls
28 lines (24 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package utils.number
{
/**
Determines if index is included within the collection length otherwise the index loops to the beginning or end of the range and continues.
@param index: Index to loop if needed.
@param length: The total elements in the collection.
@return A valid zero-based index.
@example
<code>
var colors:Array = new Array("Red", "Green", "Blue");
trace(colors[NumberUtil.loopIndex(2, colors.length)]); // Traces Blue
trace(colors[NumberUtil.loopIndex(4, colors.length)]); // Traces Green
trace(colors[NumberUtil.loopIndex(-6, colors.length)]); // Traces Red
</code>
*/
public function loopIndex(index:int, length:uint):uint
{
if (index < 0)
index = length + index % length;
if (index >= length)
return index % length;
return index;
}
}