phaserjs/phaser · error · Error

Supplied items must be elements of the same array

Error message

Supplied items must be elements of the same array

What it means

Thrown by Phaser.Utils.Array.MoveAbove when either item1 or item2 is not present in the supplied array (indexOf returns -1). MoveAbove relocates item1 to immediately after item2 (toward the end of the array), so both must be members of the same array. The guard runs after the identical-reference shortcut, so passing the same object twice is allowed; only genuinely missing elements throw.

Source

Thrown at src/utils/array/MoveAbove.js:34

 * @param {array} array - The input array.
 * @param {*} item1 - The element to move above the base element.
 * @param {*} item2 - The base element.
 *
 * @return {array} The input array.
 */
var MoveAbove = function (array, item1, item2)
{
    if (item1 === item2)
    {
        return array;
    }

    var currentIndex = array.indexOf(item1);
    var baseIndex = array.indexOf(item2);

    if (currentIndex < 0 || baseIndex < 0)
    {
        throw new Error('Supplied items must be elements of the same array');
    }

    if (currentIndex > baseIndex)
    {
        // item1 is already above item2
        return array;
    }

    // Remove item1 from its current position
    array.splice(currentIndex, 1);

    // Recalculate baseIndex after removal
    baseIndex = array.indexOf(item2);

    // Insert item1 immediately after item2
    array.splice(baseIndex + 1, 0, item1);
    
    return array;

View on GitHub (pinned to 41be1e462b)

Solutions

  1. Verify membership before calling: `if (arr.includes(a) && arr.includes(b)) MoveAbove(arr, a, b);`.
  2. Ensure both items are the exact same object references that were pushed into the array (not equivalent copies).
  3. If items may have been removed, re-fetch them from the array before reordering.
  4. Consider Phaser.Utils.Array.MoveTo(arr, item, newIndex) if you only know the target position.

Example fix

// before
Phaser.Utils.Array.MoveAbove(children, spriteA, spriteB); // spriteB already removed

// after
if (children.includes(spriteA) && children.includes(spriteB)) {
  Phaser.Utils.Array.MoveAbove(children, spriteA, spriteB);
}
Defensive patterns

Strategy: validation

Validate before calling

function safeMoveAbove(arr, a, b) {
  if (arr.indexOf(a) !== -1 && arr.indexOf(b) !== -1) {
    return Phaser.Utils.Array.MoveAbove(arr, a, b);
  }
  return arr;
}

Type guard

function areSameArrayMembers(arr, a, b) {
  return arr.indexOf(a) !== -1 && arr.indexOf(b) !== -1;
}

Try / catch

try {
  Phaser.Utils.Array.MoveAbove(arr, a, b);
} catch (e) {
  if (e.message !== 'Supplied items must be elements of the same array') throw e;
  // graceful no-op: one item is no longer present
}

Prevention

When it happens

Trigger: Calling MoveAbove(arr, a, b) where arr.indexOf(a) === -1 or arr.indexOf(b) === -1. Commonly when one item was already spliced out, when comparing value-types that look equal but are different references (indexOf uses ===), or when passing an item from a sibling array by mistake.

Common situations: Reordering a display list or children array after a remove operation moved an element. Using MoveAbove on a copy of an array while the items reference the original. Comparing objects by value when indexOf compares by reference. Race between an async removal and the MoveAbove call. Using primitive duplicates (two separate strings/numbers with the same value works by === but two separate objects with equal shape do not).

Related errors


AI-assisted analysis of phaserjs/phaser@41be1e462b (2026-08-13). Data as JSON: /api/errors/c1a7c9ec2dd8910f. Report an issue: GitHub.