new ArrayUtils()
Utility functions for dealing with Arrays.
- Source - utils/ArrayUtils.js, line 13
Methods
-
<static> findClosest(value, arr) → {number}
-
Snaps a value to the nearest value in an array.
The result will always be in the range[first_value, last_value].Parameters:
Name Type Description valuenumber The search value
arrArray.<number> The input array which must be sorted.
Returns:
number -The nearest value found.
- Source - utils/ArrayUtils.js, line 163
-
<static> getRandomItem(objects, startIndex, length) → {object}
-
Fetch a random entry from the given array.
Will return null if there are no array items that fall within the specified range
or if there is no item for the randomly chosen index.Parameters:
Name Type Description objectsArray.<any> An array of objects.
startIndexinteger Optional offset off the front of the array. Default value is 0, or the beginning of the array.
lengthinteger Optional restriction on the number of values you want to randomly select from.
Returns:
object -The random object that was selected.
- Source - utils/ArrayUtils.js, line 15
-
<static> numberArray(start, end) → {Array.<number>}
-
Create an array representing the inclusive range of numbers (usually integers) in
[start, end].
This is equivalent tonumberArrayStep(start, end, 1).Parameters:
Name Type Description startnumber The minimum value the array starts with.
endnumber The maximum value the array contains.
Returns:
Array.<number> -The array of number values.
- Source - utils/ArrayUtils.js, line 262
-
<static> numberArrayStep(start, end, step) → {Array}
-
Create an array of numbers (positive and/or negative) progressing from
start
up to but not includingendby advancing bystep.If
startis less thanenda zero-length range is created unless a negativestepis specified.Certain values for
startandend(eg. NaN/undefined/null) are currently coerced to 0;
for forward compatibility make sure to pass in actual numbers.Parameters:
Name Type Argument Default Description startnumber The start of the range.
endnumber <optional>
The end of the range.
stepnumber <optional>
1 The value to increment or decrement by.
Returns:
Array -Returns the new array of numbers.
- Source - utils/ArrayUtils.js, line 284
Example
Phaser.ArrayUtils.numberArrayStep(4); // => [0, 1, 2, 3] Phaser.ArrayUtils.numberArrayStep(1, 5); // => [1, 2, 3, 4] Phaser.ArrayUtils.numberArrayStep(0, 20, 5); // => [0, 5, 10, 15] Phaser.ArrayUtils.numberArrayStep(0, -4, -1); // => [0, -1, -2, -3] Phaser.ArrayUtils.numberArrayStep(1, 4, 0); // => [1, 1, 1] Phaser.ArrayUtils.numberArrayStep(0); // => []
-
<static> removeRandomItem(objects, startIndex, length) → {object}
-
Removes a random object from the given array and returns it.
Will return null if there are no array items that fall within the specified range
or if there is no item for the randomly chosen index.Parameters:
Name Type Description objectsArray.<any> An array of objects.
startIndexinteger Optional offset off the front of the array. Default value is 0, or the beginning of the array.
lengthinteger Optional restriction on the number of values you want to randomly select from.
Returns:
object -The random object that was removed.
- Source - utils/ArrayUtils.js, line 39
-
<static> rotate(array) → {any}
- This method is deprecated and should not be used. It may be removed in the future.
- Deprecated:
- Please use Phaser.ArrayUtils.rotate instead.
- Source - utils/ArrayUtils.js, line 239
-
<static> rotateLeft(array) → {any}
-
Moves the element from the start of the array to the end, shifting all items in the process.
The "rotation" happens to the left.Before:
[ A, B, C, D, E, F ]
After:[ B, C, D, E, F, A ]See also Phaser.ArrayUtils.rotateRight
Parameters:
Name Type Description arrayArray.<any> The array to rotate. The array is modified.
Returns:
any -The rotated value.
- Source - utils/ArrayUtils.js, line 217
-
<static> rotateMatrix(matrix, direction) → {Array.<Array.<any>>}
-
Rotates the given matrix (array of arrays).
Based on the routine from http://jsfiddle.net/MrPolywhirl/NH42z/.
Parameters:
Name Type Description matrixArray.<Array.<any>> The array to rotate; this matrix may be altered.
directionnumber | string The amount to rotate: the rotation in degrees (90, -90, 270, -270, 180) or a string command ('rotateLeft', 'rotateRight' or 'rotate180').
Returns:
Array.<Array.<any>> -The rotated matrix. The source matrix should be discarded for the returned matrix.
- Source - utils/ArrayUtils.js, line 122
-
<static> rotateRight(array) → {any}
-
Moves the element from the end of the array to the start, shifting all items in the process.
The "rotation" happens to the right.Before:
[ A, B, C, D, E, F ]
After:[ F, A, B, C, D, E ]See also Phaser.ArrayUtils.rotateLeft.
Parameters:
Name Type Description arrayArray.<any> The array to rotate. The array is modified.
Returns:
any -The shifted value.
- Source - utils/ArrayUtils.js, line 195
-
<static> shuffle(array) → {Array.<any>}
-
A standard Fisher-Yates Array shuffle implementation which modifies the array in place.
Parameters:
Name Type Description arrayArray.<any> The array to shuffle.
Returns:
Array.<any> -The original array, now shuffled.
- Source - utils/ArrayUtils.js, line 73
-
<static> transposeMatrix(array) → {Array.<Array.<any>>}
-
Transposes the elements of the given matrix (array of arrays).
Parameters:
Name Type Description arrayArray.<Array.<any>> The matrix to transpose.
Returns:
Array.<Array.<any>> -A new transposed matrix
- Source - utils/ArrayUtils.js, line 94
Moves the element from the start of the array to the end, shifting all items in the process.
The "rotation" happens to the left.
Before: [ A, B, C, D, E, F ]
After: [ B, C, D, E, F, A ]
See also Phaser.ArrayUtils.rotateRight
Parameters:
| Name | Type | Description |
|---|---|---|
array |
Array.<any> | The array to rotate. The array is modified. |
Returns:
The rotated value.
