Navigation
These archived docs are for Phaser 2.2.2 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. ArrayUtils

new ArrayUtils()

Utility functions for dealing with Arrays.

Source code: utils/ArrayUtils.js, line 13

Public 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
value number

The search value

arr number[]

The input array which must be sorted.

Returns
number -

The nearest value found.

Source code: utils/ArrayUtils.js, line 174

<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 choosen index.

Parameters
Name Type Description
objects 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.

Returns
object -

The random object that was selected.

Source code: utils/ArrayUtils.js, line 27

<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 choosen index.

Parameters
Name Type Description
objects 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.

Returns
object -

The random object that was removed.

Source code: utils/ArrayUtils.js, line 53

<static> rotate(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.

Parameters
Name Type Description
array any[]

The array to shift/rotate. The array is modified.

Returns
any -

The shifted value.

Source code: utils/ArrayUtils.js, line 197

<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
matrix Array.<Array.<any>>

The array to rotate; this matrix may be altered.

direction number | string

The amount to rotate: the roation 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 code: utils/ArrayUtils.js, line 134

<static> shuffle(array) → {any[]}

A standard Fisher-Yates Array shuffle implementation which modifies the array in place.

Parameters
Name Type Description
array any[]

The array to shuffle.

Returns
any[] -

The original array, now shuffled.

Source code: utils/ArrayUtils.js, line 82

<static> transposeMatrix(array) → {Array.<Array.<any>>}

Transposes the elements of the given matrix (array of arrays).

Parameters
Name Type Description
array Array.<Array.<any>>

The matrix to transpose.

Returns
Array.<Array.<any>> -

A new transposed matrix

Source code: utils/ArrayUtils.js, line 103