phaserjs/phaser · error · Error
Index out of bounds
Error message
Index out of bounds
What it means
Thrown by Phaser.Utils.Array.RemoveAt when the supplied index is negative or greater than the last valid index (array.length - 1). RemoveAt splices one element out by position and optionally fires a callback; the bounds check protects SpliceOne from a no-op or corrupting write. The check is strict on both ends: index 0 is valid, index length-1 is valid, anything outside throws.
Source
Thrown at src/utils/array/RemoveAt.js:32
* You can optionally specify a callback to be invoked for the item if it is successfully removed from the array.
*
* @function Phaser.Utils.Array.RemoveAt
* @since 3.4.0
*
* @param {array} array - The array to be modified.
* @param {number} index - The array index to remove the item from. The index must be in bounds or it will throw an error.
* @param {function} [callback] - A callback to be invoked for the item removed from the array.
* @param {object} [context] - The context in which the callback is invoked.
*
* @return {*} The item that was removed.
*/
var RemoveAt = function (array, index, callback, context)
{
if (context === undefined) { context = array; }
if (index < 0 || index > array.length - 1)
{
throw new Error('Index out of bounds');
}
var item = SpliceOne(array, index);
if (callback)
{
callback.call(context, item);
}
return item;
};
module.exports = RemoveAt;
View on GitHub (pinned to 41be1e462b)
Solutions
- Bounds-check first: `if (index >= 0 && index < arr.length) RemoveAt(arr, index);`.
- When removing during iteration, iterate backward and recompute length each step.
- Prefer RemoveAt(arr, arr.indexOf(item)) only after confirming indexOf !== -1.
- For filtering by predicate, use Array.filter or Phaser.Utils.Array.RemoveEach instead of indexed removal.
Example fix
// before
for (var i = 0; i < list.length; i++) {
if (shouldRemove(list[i])) Phaser.Utils.Array.RemoveAt(list, i);
}
// after
for (var i = list.length - 1; i >= 0; i--) {
if (shouldRemove(list[i])) Phaser.Utils.Array.RemoveAt(list, i);
} Defensive patterns
Strategy: validation
Validate before calling
function safeRemoveAt(arr, index, cb, ctx) {
if (index >= 0 && index < arr.length) {
return Phaser.Utils.Array.RemoveAt(arr, index, cb, ctx);
}
return undefined;
} Type guard
function isInBounds(arr, index) {
return Number.isInteger(index) && index >= 0 && index < arr.length;
} Try / catch
try {
Phaser.Utils.Array.RemoveAt(arr, i);
} catch (e) {
if (e.message === 'Index out of bounds') {
// list shrank; recompute and skip
} else { throw e; }
} Prevention
- Bounds-check index against arr.length before calling.
- Iterate backward when removing during a loop.
- Recompute length each iteration when the array mutates.
- Prefer predicate-based removal (Array.filter, Phaser.Utils.Array.RemoveEach) over indexed removal.
When it happens
Trigger: Calling RemoveAt(arr, -1), RemoveAt(arr, arr.length), or RemoveAt on an array whose length changed between computing the index and the call. Also RemoveAt([], 0) since length-1 is -1 and 0 > -1.
Common situations: Iterating an array backward with RemoveAt inside a loop and miscalculating the current index. Removing during a forEach/map where the index comes from a stale closure. Reading index from user input or a server response without bounds checking. Destroying a Phaser GameObject and then trying to RemoveAt its display-list index after the list shrank.
Related errors
- Supplied index out of bounds
- Range Error: Values outside acceptable range
- Supplied items must be elements of the same array
- Supplied items must be elements of the same array
- Supplied items must be elements of the same array
AI-assisted analysis of phaserjs/phaser@41be1e462b (2026-08-13).
Data as JSON: /api/errors/897377d4f5bd4dfd.
Report an issue: GitHub.