Namespace: Array

Phaser.Utils. Array

Namespaces

Matrix

Methods


<static> Add(array, item [, limit] [, callback] [, context])

Adds the given item, or array of items, to the array.

Each item must be unique within the array.

The array is modified in-place and returned.

You can optionally specify a limit to the maximum size of the array. If the quantity of items being added will take the array length over this limit, it will stop adding once the limit is reached.

You can optionally specify a callback to be invoked for each item successfully added to the array.

Parameters:
Name Type Argument Description
array array

The array to be added to.

item any | Array.<any>

The item, or array of items, to add to the array. Each item must be unique within the array.

limit number <optional>

Optional limit which caps the size of the array.

callback function <optional>

A callback to be invoked for each item successfully added to the array.

context object <optional>

The context in which the callback is invoked.

Since: 3.4.0
Source: src/utils/array/Add.js (Line 7)
Returns:

The input array.

Type
array

<static> AddAt(array, item [, index] [, limit] [, callback] [, context])

Adds the given item, or array of items, to the array starting at the index specified.

Each item must be unique within the array.

Existing elements in the array are shifted up.

The array is modified in-place and returned.

You can optionally specify a limit to the maximum size of the array. If the quantity of items being added will take the array length over this limit, it will stop adding once the limit is reached.

You can optionally specify a callback to be invoked for each item successfully added to the array.

Parameters:
Name Type Argument Default Description
array array

The array to be added to.

item any | Array.<any>

The item, or array of items, to add to the array.

index number <optional>
0

The index in the array where the item will be inserted.

limit number <optional>

Optional limit which caps the size of the array.

callback function <optional>

A callback to be invoked for each item successfully added to the array.

context object <optional>

The context in which the callback is invoked.

Since: 3.4.0
Source: src/utils/array/AddAt.js (Line 7)
Returns:

The input array.

Type
array

<static> BringToTop(array, item)

Moves the given element to the top of the array. The array is modified in-place.

Parameters:
Name Type Description
array array

The array.

item *

The element to move.

Since: 3.4.0
Source: src/utils/array/BringToTop.js (Line 7)
Returns:

The element that was moved.

Type
*

<static> CountAllMatching(array, property, value [, startIndex] [, endIndex])

Returns the total number of elements in the array which have a property matching the given value.

Parameters:
Name Type Argument Description
array array

The array to search.

property string

The property to test on each array element.

value *

The value to test the property against. Must pass a strict (===) comparison check.

startIndex number <optional>

An optional start index to search from.

endIndex number <optional>

An optional end index to search to.

Since: 3.4.0
Source: src/utils/array/CountAllMatching.js (Line 9)
Returns:

The total number of elements with properties matching the given value.

Type
number

<static> Each(array, callback, context [, args])

Passes each element in the array to the given callback.

Parameters:
Name Type Argument Description
array array

The array to search.

callback function

A callback to be invoked for each item in the array.

context object

The context in which the callback is invoked.

args * <optional>
<repeatable>

Additional arguments that will be passed to the callback, after the current array item.

Since: 3.4.0
Source: src/utils/array/Each.js (Line 7)
Returns:

The input array.

Type
array

<static> EachInRange(array, callback, context, startIndex, endIndex [, args])

Passes each element in the array, between the start and end indexes, to the given callback.

Parameters:
Name Type Argument Description
array array

The array to search.

callback function

A callback to be invoked for each item in the array.

context object

The context in which the callback is invoked.

startIndex number

The start index to search from.

endIndex number

The end index to search to.

args * <optional>
<repeatable>

Additional arguments that will be passed to the callback, after the child.

Since: 3.4.0
Source: src/utils/array/EachInRange.js (Line 9)
Returns:

The input array.

Type
array

<static> FindClosestInSorted(value, array [, key])

Searches a pre-sorted array for the closet value to the given number.

If the key argument is given it will assume the array contains objects that all have the required key property name, and will check for the closest value of those to the given number.

Parameters:
Name Type Argument Description
value number

The value to search for in the array.

array array

The array to search, which must be sorted.

key string <optional>

An optional property key. If specified the array elements property will be checked against value.

Since: 3.0.0
Source: src/utils/array/FindClosestInSorted.js (Line 7)
Returns:

The nearest value found in the array, or if a key was given, the nearest object with the matching property value.

Type
number | any

<static> GetAll(array [, property] [, value] [, startIndex] [, endIndex])

Returns all elements in the array.

You can optionally specify a matching criteria using the property and value arguments.

For example: getAll('visible', true) would return only elements that have their visible property set.

Optionally you can specify a start and end index. For example if the array had 100 elements, and you set startIndex to 0 and endIndex to 50, it would return matches from only the first 50 elements.

Parameters:
Name Type Argument Description
array array

The array to search.

property string <optional>

The property to test on each array element.

value * <optional>

The value to test the property against. Must pass a strict (===) comparison check.

startIndex number <optional>

An optional start index to search from.

endIndex number <optional>

An optional end index to search to.

Since: 3.4.0
Source: src/utils/array/GetAll.js (Line 9)
Returns:

All matching elements from the array.

Type
array

<static> GetFirst(array [, property] [, value] [, startIndex] [, endIndex])

Returns the first element in the array.

You can optionally specify a matching criteria using the property and value arguments.

For example: getAll('visible', true) would return the first element that had its visible property set.

Optionally you can specify a start and end index. For example if the array had 100 elements, and you set startIndex to 0 and endIndex to 50, it would search only the first 50 elements.

Parameters:
Name Type Argument Default Description
array array

The array to search.

property string <optional>

The property to test on each array element.

value * <optional>

The value to test the property against. Must pass a strict (===) comparison check.

startIndex number <optional>
0

An optional start index to search from.

endIndex number <optional>
array.length

An optional end index to search up to (but not included)

Since: 3.4.0
Source: src/utils/array/GetFirst.js (Line 9)
Returns:

The first matching element from the array, or null if no element could be found in the range given.

Type
object

<static> GetRandom(array [, startIndex] [, length])

Returns a Random element from the array.

Parameters:
Name Type Argument Default Description
array array

The array to select the random entry from.

startIndex number <optional>
0

An optional start index.

length number <optional>
array.length

An optional length, the total number of elements (from the startIndex) to choose from.

Since: 3.0.0
Source: src/utils/array/GetRandom.js (Line 7)
Returns:

A random element from the array, or null if no element could be found in the range given.

Type
*

<static> MoveDown(array, item)

Moves the given array element down one place in the array. The array is modified in-place.

Parameters:
Name Type Description
array array

The input array.

item *

The element to move down the array.

Since: 3.4.0
Source: src/utils/array/MoveDown.js (Line 7)
Returns:

The input array.

Type
array

<static> MoveTo(array, item, index)

Moves an element in an array to a new position within the same array. The array is modified in-place.

Parameters:
Name Type Description
array array

The array.

item *

The element to move.

index number

The new index that the element will be moved to.

Since: 3.4.0
Source: src/utils/array/MoveTo.js (Line 7)
Returns:

The element that was moved.

Type
*

<static> MoveUp(array, item)

Moves the given array element up one place in the array. The array is modified in-place.

Parameters:
Name Type Description
array array

The input array.

item *

The element to move up the array.

Since: 3.4.0
Source: src/utils/array/MoveUp.js (Line 7)
Returns:

The input array.

Type
array

<static> NumberArray(start, end [, prefix] [, suffix])

Create an array representing the range of numbers (usually integers), between, and inclusive of, the given start and end arguments. For example:

var array = Phaser.Utils.Array.NumberArray(2, 4); // array = [2, 3, 4] var array = Phaser.Utils.Array.NumberArray(0, 9); // array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] var array = Phaser.Utils.Array.NumberArray(8, 2); // array = [8, 7, 6, 5, 4, 3, 2]

This is equivalent to Phaser.Utils.Array.NumberArrayStep(start, end, 1).

You can optionally provide a prefix and / or suffix string. If given the array will contain strings, not integers. For example:

var array = Phaser.Utils.Array.NumberArray(1, 4, 'Level '); // array = ["Level 1", "Level 2", "Level 3", "Level 4"] var array = Phaser.Utils.Array.NumberArray(5, 7, 'HD-', '.png'); // array = ["HD-5.png", "HD-6.png", "HD-7.png"]

Parameters:
Name Type Argument Description
start number

The minimum value the array starts with.

end number

The maximum value the array contains.

prefix string <optional>

Optional prefix to place before the number. If provided the array will contain strings, not integers.

suffix string <optional>

Optional suffix to place after the number. If provided the array will contain strings, not integers.

Since: 3.0.0
Source: src/utils/array/NumberArray.js (Line 7)
Returns:

The array of number values, or strings if a prefix or suffix was provided.

Type
Array.<number> | Array.<string>

<static> NumberArrayStep( [start] [, end] [, step])

Create an array of numbers (positive and/or negative) progressing from start up to but not including end by advancing by step.

If start is less than end a zero-length range is created unless a negative step is specified.

Certain values for start and end (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 <optional>
0

The start of the range.

end number <optional>
null

The end of the range.

step number <optional>
1

The value to increment or decrement by.

Since: 3.0.0
Source: src/utils/array/NumberArrayStep.js (Line 9)
Returns:

The array of number values.

Type
Array.<number>
Example
NumberArrayStep(4);
// => [0, 1, 2, 3]

NumberArrayStep(1, 5);
// => [1, 2, 3, 4]

NumberArrayStep(0, 20, 5);
// => [0, 5, 10, 15]

NumberArrayStep(0, -4, -1);
// => [0, -1, -2, -3]

NumberArrayStep(1, 4, 0);
// => [1, 1, 1]

NumberArrayStep(0);
// => []

<static> QuickSelect(arr, k [, left] [, right] [, compare])

A Floyd-Rivest quick selection algorithm.

Rearranges the array items so that all items in the [left, k] range are smaller than all items in [k, right]; The k-th element will have the (k - left + 1)th smallest value in [left, right].

The array is modified in-place.

Based on code by Vladimir Agafonkin

Parameters:
Name Type Argument Default Description
arr array

The array to sort.

k number

The k-th element index.

left number <optional>
0

The index of the left part of the range.

right number <optional>

The index of the right part of the range.

compare function <optional>

An optional comparison function. Is passed two elements and should return 0, 1 or -1.

Since: 3.0.0
Source: src/utils/array/QuickSelect.js (Line 25)

<static> Range(a, b [, options])

Creates an array populated with a range of values, based on the given arguments and configuration object.

Range ([a,b,c], [1,2,3]) = a1, a2, a3, b1, b2, b3, c1, c2, c3

Range ([a,b], [1,2,3], qty = 3) = a1, a1, a1, a2, a2, a2, a3, a3, a3, b1, b1, b1, b2, b2, b2, b3, b3, b3

Range ([a,b,c], [1,2,3], repeat x1) = a1, a2, a3, b1, b2, b3, c1, c2, c3, a1, a2, a3, b1, b2, b3, c1, c2, c3

Range ([a,b], [1,2], repeat -1 = endless, max = 14) = Maybe if max is set then repeat goes to -1 automatically? a1, a2, b1, b2, a1, a2, b1, b2, a1, a2, b1, b2, a1, a2 (capped at 14 elements)

Range ([a], [1,2,3,4,5], random = true) = a4, a1, a5, a2, a3

Range ([a, b], [1,2,3], random = true) = b3, a2, a1, b1, a3, b2

Range ([a, b, c], [1,2,3], randomB = true) = a3, a1, a2, b2, b3, b1, c1, c3, c2

Range ([a], [1,2,3,4,5], yoyo = true) = a1, a2, a3, a4, a5, a5, a4, a3, a2, a1

Range ([a, b], [1,2,3], yoyo = true) = a1, a2, a3, b1, b2, b3, b3, b2, b1, a3, a2, a1

Parameters:
Name Type Argument Description
a array

The first array of range elements.

b array

The second array of range elements.

options object <optional>

A range configuration object. Can contain: repeat, random, randomB, yoyo, max, qty.

Since: 3.0.0
Source: src/utils/array/Range.js (Line 28)
Returns:

An array of arranged elements.

Type
array

<static> Remove(array, item [, callback] [, context])

Removes the given item, or array of items, from the array.

The array is modified in-place.

You can optionally specify a callback to be invoked for each item successfully removed from the array.

Parameters:
Name Type Argument Description
array array

The array to be modified.

item * | Array.<*>

The item, or array of items, to be removed from the array.

callback function <optional>

A callback to be invoked for each item successfully removed from the array.

context object <optional>

The context in which the callback is invoked.

Since: 3.4.0
Source: src/utils/array/Remove.js (Line 9)
Returns:

The item, or array of items, that were successfully removed from the array.

Type
* | Array.<*>

<static> RemoveAt(array, index [, callback] [, context])

Removes the item from the given position in the array.

The array is modified in-place.

You can optionally specify a callback to be invoked for the item if it is successfully removed from the array.

Parameters:
Name Type Argument Description
array array

The array to be modified.

index number

The array index to remove the item from. The index must be in bounds or it will throw an error.

callback function <optional>

A callback to be invoked for the item removed from the array.

context object <optional>

The context in which the callback is invoked.

Since: 3.4.0
Source: src/utils/array/RemoveAt.js (Line 9)
Returns:

The item that was removed.

Type
*

<static> RemoveBetween(array, startIndex, endIndex [, callback] [, context])

Removes the item within the given range in the array.

The array is modified in-place.

You can optionally specify a callback to be invoked for the item/s successfully removed from the array.

Parameters:
Name Type Argument Description
array array

The array to be modified.

startIndex number

The start index to remove from.

endIndex number

The end index to remove to.

callback function <optional>

A callback to be invoked for the item removed from the array.

context object <optional>

The context in which the callback is invoked.

Since: 3.4.0
Source: src/utils/array/RemoveBetween.js (Line 9)
Returns:

An array of items that were removed.

Type
Array.<*>

<static> RemoveRandomElement(array [, start] [, 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 Argument Default Description
array array

The array to removed a random element from.

start number <optional>
0

The array index to start the search from.

length number <optional>
array.length

Optional restriction on the number of elements to randomly select from.

Since: 3.0.0
Source: src/utils/array/RemoveRandomElement.js (Line 9)
Returns:

The random element that was removed, or null if there were no array elements that fell within the given range.

Type
object

<static> Replace(array, oldChild, newChild)

Replaces an element of the array with the new element. The new element cannot already be a member of the array. The array is modified in-place.

Parameters:
Name Type Description
array array

The array to search within.

oldChild *

The element in the array that will be replaced.

newChild *

The element to be inserted into the array at the position of oldChild.

Since: 3.4.0
Source: src/utils/array/Replace.js (Line 7)
Returns:

Returns true if the oldChild was successfully replaced, otherwise returns false.

Type
boolean

<static> RotateLeft(array [, total])

Moves the element at the start of the array to the end, shifting all items in the process. The "rotation" happens to the left.

Parameters:
Name Type Argument Default Description
array array

The array to shift to the left. This array is modified in place.

total number <optional>
1

The number of times to shift the array.

Since: 3.0.0
Source: src/utils/array/RotateLeft.js (Line 7)
Returns:

The most recently shifted element.

Type
*

<static> RotateRight(array [, total])

Moves the element at the end of the array to the start, shifting all items in the process. The "rotation" happens to the right.

Parameters:
Name Type Argument Default Description
array array

The array to shift to the right. This array is modified in place.

total number <optional>
1

The number of times to shift the array.

Since: 3.0.0
Source: src/utils/array/RotateRight.js (Line 7)
Returns:

The most recently shifted element.

Type
*

<static> SafeRange(array, startIndex, endIndex [, throwError])

Tests if the start and end indexes are a safe range for the given array.

Parameters:
Name Type Argument Default Description
array array

The array to check.

startIndex number

The start index.

endIndex number

The end index.

throwError boolean <optional>
true

Throw an error if the range is out of bounds.

Since: 3.4.0
Source: src/utils/array/SafeRange.js (Line 7)
Returns:

True if the range is safe, otherwise false.

Type
boolean

<static> SendToBack(array, item)

Moves the given element to the bottom of the array. The array is modified in-place.

Parameters:
Name Type Description
array array

The array.

item *

The element to move.

Since: 3.4.0
Source: src/utils/array/SendToBack.js (Line 7)
Returns:

The element that was moved.

Type
*

<static> SetAll(array, property, value [, startIndex] [, endIndex])

Scans the array for elements with the given property. If found, the property is set to the value.

For example: SetAll('visible', true) would set all elements that have a visible property to false.

Optionally you can specify a start and end index. For example if the array had 100 elements, and you set startIndex to 0 and endIndex to 50, it would update only the first 50 elements.

Parameters:
Name Type Argument Description
array array

The array to search.

property string

The property to test for on each array element.

value *

The value to set the property to.

startIndex number <optional>

An optional start index to search from.

endIndex number <optional>

An optional end index to search to.

Since: 3.4.0
Source: src/utils/array/SetAll.js (Line 9)
Returns:

The input array.

Type
array

<static> Shuffle(array)

Shuffles the contents of the given array using the Fisher-Yates implementation.

The original array is modified directly and returned.

Parameters:
Name Type Description
array Array.<T>

The array to shuffle. This array is modified in place.

Since: 3.0.0
Source: src/utils/array/Shuffle.js (Line 7)
Returns:

The shuffled array.

Type
Array.<T>

<static> SortByDigits(array)

Takes the given array and runs a numeric sort on it, ignoring any non-digits that may be in the entries.

You should only run this on arrays containing strings.

Parameters:
Name Type Description
array Array.<string>

The input array of strings.

Since: 3.50.0
Source: src/utils/array/SortByDigits.js (Line 7)
Returns:

The sorted input array.

Type
Array.<string>

<static> SpliceOne(array, index)

Removes a single item from an array and returns it without creating gc, like the native splice does. Based on code by Mike Reinstein.

Parameters:
Name Type Description
array array

The array to splice from.

index number

The index of the item which should be spliced.

Since: 3.0.0
Source: src/utils/array/SpliceOne.js (Line 7)
Returns:

The item which was spliced (removed).

Type
*

<static> StableSort(array [, compare])

An in-place stable array sort, because Array#sort() is not guaranteed stable.

This is an implementation of merge sort, without recursion.

Function based on the Two-Screen/stable sort 0.1.8 from https://github.com/Two-Screen/stable

Parameters:
Name Type Argument Description
array array

The input array to be sorted.

compare function <optional>

The comparison function.

Since: 3.0.0
Source: src/utils/array/StableSort.js (Line 140)
Returns:

The sorted result.

Type
array

<static> Swap(array, item1, item2)

Swaps the position of two elements in the given array. The elements must exist in the same array. The array is modified in-place.

Parameters:
Name Type Description
array array

The input array.

item1 *

The first element to swap.

item2 *

The second element to swap.

Since: 3.4.0
Source: src/utils/array/Swap.js (Line 7)
Returns:

The input array.

Type
array