Phaser API Documentation

  Version: 
Filter
Namespace: Array
Phaser.Utils.Array
Source: src/utils/array/index.js (Line 7)

Namespace

Methods

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

Description:

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 arguments 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.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Source: src/utils/array/Add.js (Line 7)
Focus
Focus
<static> AddAt(array, item, [index], [limit], [callback], [context])
Focus
Focus

Description:

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 arguments 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>

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.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Source: src/utils/array/AddAt.js (Line 7)
Focus
Focus
<static> BringToTop(array, item)
Focus
Focus

Description:

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.

Returns:
Description:

The element that was moved.

Type:
  • *
Since: 3.4.0
Focus
Focus
<static> CountAllMatching(array, property, value, [startIndex], [endIndex])
Focus
Focus

Description:

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

Parameters:

name type arguments 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.

Returns:
Description:

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

Type:
  • number
Since: 3.4.0
Focus
Focus
<static> Each(array, callback, context, [args])
Focus
Focus

Description:

Passes each element in the array to the given callback.

Parameters:

name type arguments 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>

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

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Source: src/utils/array/Each.js (Line 7)
Focus
Focus
<static> EachInRange(array, callback, context, startIndex, endIndex, [args])
Focus
Focus

Description:

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

Parameters:

name type arguments 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>

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

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Focus
Focus
<static> FindClosestInSorted(value, array, [key])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

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

Type:
  • number
  • any
Since: 3.0.0
Focus
Focus
<static> GetAll(array, [property], [value], [startIndex], [endIndex])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

All matching elements from the array.

Type:
  • array
Since: 3.4.0
Source: src/utils/array/GetAll.js (Line 9)
Focus
Focus
<static> GetFirst(array, [property], [value], [startIndex], [endIndex])
Focus
Focus

Description:

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 arguments 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)

Returns:
Description:

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

Type:
  • object
Since: 3.4.0
Focus
Focus
<static> GetRandom(array, [startIndex], [length])
Focus
Focus

Description:

Returns a Random element from the array.

Parameters:

name type arguments 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.

Returns:
Description:

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

Type:
  • *
Since: 3.0.0
Focus
Focus
<static> MoveAbove(array, item1, item2)
Focus
Focus

Description:

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

Parameters:

name type description
array array

The input array.

item1 *

The element to move above base element.

item2 *

The base element.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.55.0
Focus
Focus
<static> MoveBelow(array, item1, item2)
Focus
Focus

Description:

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

Parameters:

name type description
array array

The input array.

item1 *

The element to move below base element.

item2 *

The base element.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.55.0
Focus
Focus
<static> MoveDown(array, item)
Focus
Focus

Description:

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.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Focus
Focus
<static> MoveTo(array, item, index)
Focus
Focus

Description:

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.

Returns:
Description:

The element that was moved.

Type:
  • *
Since: 3.4.0
Source: src/utils/array/MoveTo.js (Line 7)
Focus
Focus
<static> MoveUp(array, item)
Focus
Focus

Description:

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.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Source: src/utils/array/MoveUp.js (Line 7)
Focus
Focus
<static> NumberArray(start, end, [prefix], [suffix])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

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

Type:
  • Array.<number>
  • Array.<string>
Since: 3.0.0
Focus
Focus
<static> NumberArrayStep([start], [end], [step])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

The array of number values.

Type:
  • Array.<number>
Examples:
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);
// => []
Since: 3.0.0
Focus
Focus
<static> QuickSelect(arr, k, [left], [right], [compare])
Focus
Focus

Description:

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 arguments description
arr array

The array to sort.

k number

The k-th element index.

left number <optional>

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
Focus
Focus
<static> Range(a, b, [options])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

An array of arranged elements.

Type:
  • array
Since: 3.0.0
Source: src/utils/array/Range.js (Line 28)
Focus
Focus
<static> Remove(array, item, [callback], [context])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

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

Type:
  • *
  • Array.<*>
Since: 3.4.0
Source: src/utils/array/Remove.js (Line 9)
Focus
Focus
<static> RemoveAt(array, index, [callback], [context])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

The item that was removed.

Type:
  • *
Since: 3.4.0
Focus
Focus
<static> RemoveBetween(array, startIndex, endIndex, [callback], [context])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

An array of items that were removed.

Type:
  • Array.<*>
Since: 3.4.0
Focus
Focus
<static> RemoveRandomElement(array, [start], [length])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

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

Type:
  • object
Since: 3.0.0
Focus
Focus
<static> Replace(array, oldChild, newChild)
Focus
Focus

Description:

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.

Returns:
Description:

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

Type:
  • boolean
Since: 3.4.0
Source: src/utils/array/Replace.js (Line 7)
Focus
Focus
<static> RotateLeft(array, [total])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

The most recently shifted element.

Type:
  • *
Since: 3.0.0
Focus
Focus
<static> RotateRight(array, [total])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

The most recently shifted element.

Type:
  • *
Since: 3.0.0
Focus
Focus
<static> SafeRange(array, startIndex, endIndex, [throwError])
Focus
Focus

Description:

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

Parameters:

name type arguments 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.

Returns:
Description:

True if the range is safe, otherwise false.

Type:
  • boolean
Since: 3.4.0
Focus
Focus
<static> SendToBack(array, item)
Focus
Focus

Description:

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.

Returns:
Description:

The element that was moved.

Type:
  • *
Since: 3.4.0
Focus
Focus
<static> SetAll(array, property, value, [startIndex], [endIndex])
Focus
Focus

Description:

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 arguments 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.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Source: src/utils/array/SetAll.js (Line 9)
Focus
Focus
<static> Shuffle(array)
Focus
Focus

Description:

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.

Returns:
Description:

The shuffled array.

Type:
  • Array.<T>
Since: 3.0.0
Source: src/utils/array/Shuffle.js (Line 7)
Focus
Focus
<static> SortByDigits(array)
Focus
Focus

Description:

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.

Returns:
Description:

The sorted input array.

Type:
  • Array.<string>
Since: 3.50.0
Focus
Focus
<static> SpliceOne(array, index)
Focus
Focus

Description:

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.

Returns:
Description:

The item which was spliced (removed).

Type:
  • *
Since: 3.0.0
Focus
Focus
<static> StableSort(array, [compare])
Focus
Focus

Description:

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 arguments description
array array

The input array to be sorted.

compare function <optional>

The comparison function.

Returns:
Description:

The sorted result.

Type:
  • array
Since: 3.0.0
Focus
Focus
<static> Swap(array, item1, item2)
Focus
Focus

Description:

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.

Returns:
Description:

The input array.

Type:
  • array
Since: 3.4.0
Source: src/utils/array/Swap.js (Line 7)
Focus
Focus