new ArrayUtils()
Utility functions for dealing with Arrays.
- Source:
- src/utils/ArrayUtils.js line 13
Methods
-
<static> findClosest(value, arr)
-
Snaps a value to the nearest value in a sorted numeric array. The result will always be in the range
[first_value, last_value]
.Parameters:
Name Type Description value
number The search value
arr
Array.<number> The input array which must be sorted.
- Source:
- src/utils/ArrayUtils.js line 193
Returns:
The nearest value found.
- Type
- number
-
<static> getRandomItem(objects, startIndex, length)
-
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 objects
Array.<any> An array of objects.
startIndex
integer Optional offset off the front of the array. Default value is 0, or the beginning of the array.
length
integer Optional restriction on the number of values you want to randomly select from.
- Source:
- src/utils/ArrayUtils.js line 15
Returns:
The random object that was selected.
- Type
- object
-
<static> numberArray(start [, end])
-
Create an array representing the inclusive range of numbers (usually integers) in
[start, end]
(or[0, start]
, ifend
is omitted). This is equivalent tonumberArrayStep(start, 1 + end, 1)
.When exactly one argument is passed, it's used as
end
and 0 is used asstart
. The length of the result is (1 + end).Examples
numberArray(3); // -> [0, 1, 2, 3] numberArray(0, 3); // -> [0, 1, 2, 3] numberArray(1, 3); // -> [1, 2, 3]
Parameters:
Name Type Argument Description start
number The minimum value the array starts with.
end
number <optional>
The maximum value the array contains.
- Source:
- src/utils/ArrayUtils.js line 267
Returns:
The array of number values.
- Type
- Array.<number>
-
<static> numberArrayStep(start [, end] [, step])
-
Create an array of numbers (positive and/or negative) progressing from
start
up to but not includingend
by advancing bystep
.If
start
is less thanend
a zero-length range is created unless a negativestep
is specified.Certain values for
start
andend
(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 start
number The start of the range.
end
number <optional>
The end of the range.
step
number <optional>
1 The value to increment or decrement by.
- Source:
- src/utils/ArrayUtils.js line 304
Returns:
Returns the new array of numbers.
- Type
- Array
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> remove(array, startIndex [, count])
-
Remove one or more items at the given index and reorder the array.
The new array length will be
array.length - count
.This is an alternative to
array.splice(startIndex, count)
.Parameters:
Name Type Argument Default Description array
Array.<any> startIndex
integer count
integer <optional>
1 - Source:
- src/utils/ArrayUtils.js line 72
- See:
Returns:
The modified array.
- Type
- Array.<any>
-
<static> removeRandomItem(objects, startIndex, length)
-
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 objects
Array.<any> An array of objects.
startIndex
integer Optional offset off the front of the array. Default value is 0, or the beginning of the array.
length
integer Optional restriction on the number of values you want to randomly select from.
- Source:
- src/utils/ArrayUtils.js line 38
Returns:
The random object that was removed.
- Type
- object
-
<static> rotateLeft(array)
-
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.
- Source:
- src/utils/ArrayUtils.js line 246
Returns:
The rotated value.
- Type
- any
-
<static> rotateMatrix(matrix, direction)
-
Rotates the given matrix (array of arrays).
Based on the routine from http://jsfiddle.net/MrPolywhirl/NH42z/.
Parameters:
Name Type Description matrix
Array.<Array.<any>> The array to rotate; this matrix may be altered.
direction
number | string The amount to rotate: the rotation in degrees (90, -90, 270, -270, 180) or a string command ('rotateLeft', 'rotateRight' or 'rotate180').
- Source:
- src/utils/ArrayUtils.js line 153
Returns:
The rotated matrix. The source matrix should be discarded for the returned matrix.
- Type
- Array.<Array.<any>>
-
<static> rotateRight(array)
-
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 array
Array.<any> The array to rotate. The array is modified.
- Source:
- src/utils/ArrayUtils.js line 225
Returns:
The shifted value.
- Type
- any
-
<static> shuffle(array)
-
A standard Fisher-Yates Array shuffle implementation which modifies the array in place.
Parameters:
Name Type Description array
Array.<any> The array to shuffle.
- Source:
- src/utils/ArrayUtils.js line 106
Returns:
The original array, now shuffled.
- Type
- Array.<any>
-
<static> transposeMatrix(array)
-
Transposes the elements of the given matrix (array of arrays).
Parameters:
Name Type Description array
Array.<Array.<any>> The matrix to transpose.
- Source:
- src/utils/ArrayUtils.js line 126
Returns:
A new transposed matrix
- Type
- Array.<Array.<any>>