pixi.js
    Preparing search index...

    Interface ChildrenHelperMixin<C>Advanced

    Mixin interface for containers that allows them to manage children. It provides methods for adding, removing, and manipulating child containers.

    interface ChildrenHelperMixin<C = ContainerChild> {
        addChild<U extends (C | IRenderLayer)[]>(...children: U): U[0];
        addChildAt<U>(child: U, index: number): U;
        getChildAt<U>(index: number): U;
        getChildIndex(child: C | IRenderLayer): number;
        removeChild<U extends (C | IRenderLayer)[]>(...children: U): U[0];
        removeChildAt<U>(index: number): U;
        removeChildren(beginIndex?: number, endIndex?: number): C[];
        removeFromParent(): void;
        reparentChild<U extends C[]>(...child: U): U[0];
        reparentChildAt<U>(child: U, index: number): U;
        replaceChild<U, T>(oldChild: U, newChild: T): void;
        setChildIndex(child: C | IRenderLayer, index: number): void;
        swapChildren<U>(child: U, child2: U): void;
    }

    Type Parameters

    Index

    Methods

    • Type Parameters

      • U extends (C | IRenderLayer)[]

      Parameters

      • ...children: U

      Returns U[0]

    • Adds a child to the container at a specified index. If the index is out of bounds an error will be thrown. If the child is already in this container, it will be moved to the specified index.

      Type Parameters

      • U

      Parameters

      • child: U

        The child to add

      • index: number

        The index where the child will be placed

      Returns U

      The child that was added

      // Add at specific index
      container.addChildAt(sprite, 0); // Add to front

      // Move existing child
      const index = container.children.length - 1;
      container.addChildAt(existingChild, index); // Move to back

      // With error handling
      try {
      container.addChildAt(sprite, 1000);
      } catch (e) {
      console.warn('Index out of bounds');
      }

      If index is out of bounds

    • Returns the child at the specified index.

      Type Parameters

      • U

      Parameters

      • index: number

        The index to get the child from

      Returns U

      The child at the given index

      // Get first child
      const first = container.getChildAt(0);

      // Type-safe access
      const sprite = container.getChildAt<Sprite>(1);

      // With error handling
      try {
      const child = container.getChildAt(10);
      } catch (e) {
      console.warn('Index out of bounds');
      }

      If index is out of bounds

    • Returns the index position of a child Container instance.

      Parameters

      • child: C | IRenderLayer

        The Container instance to identify

      Returns number

      The index position of the child container

      // Basic index lookup
      const index = container.getChildIndex(sprite);
      console.log(`Sprite is at index ${index}`);

      // With error handling
      try {
      const index = container.getChildIndex(sprite);
      } catch (e) {
      console.warn('Child not found in container');
      }

      If child is not in this container

    • Type Parameters

      • U extends (C | IRenderLayer)[]

      Parameters

      • ...children: U

      Returns U[0]

    • Removes a child from the specified index position.

      Type Parameters

      • U

      Parameters

      • index: number

        The index to remove the child from

      Returns U

      The child that was removed

      // Remove first child
      const removed = container.removeChildAt(0);

      // type safe access
      const sprite = container.removeChildAt<Sprite>(1);

      // With error handling
      try {
      const child = container.removeChildAt(10);
      } catch (e) {
      console.warn('Index out of bounds');
      }

      If index is out of bounds

    • Removes all children from this container that are within the begin and end indexes.

      Parameters

      • OptionalbeginIndex: number

        The beginning position

      • OptionalendIndex: number

        The ending position. Default is container size

      Returns C[]

      List of removed children

      // Remove all children
      container.removeChildren();

      // Remove first 3 children
      const removed = container.removeChildren(0, 3);
      console.log('Removed:', removed.length); // 3

      // Remove children from index 2 onwards
      container.removeChildren(2);

      // Remove specific range
      const middle = container.removeChildren(1, 4);

      If begin/end indexes are invalid

    • Remove the Container from its parent Container. If the Container has no parent, do nothing.

      Returns void

      // Basic removal
      sprite.removeFromParent();

      // With validation
      if (sprite.parent) {
      sprite.removeFromParent();
      }
    • Reparent a child or multiple children to this container while preserving their world transform. This ensures that the visual position and rotation of the children remain the same even when changing parents.

      Type Parameters

      • U extends C[]

      Parameters

      • ...child: U

        The child or children to reparent

      Returns U[0]

      The first child that was reparented

      // Basic reparenting
      const sprite = new Sprite(texture);
      oldContainer.addChild(sprite);
      // Move to new parent, keeping visual position
      newContainer.reparentChild(sprite);

      // Reparent multiple children
      const batch = [sprite1, sprite2, sprite3];
      newContainer.reparentChild(...batch);
    • Reparent the child to this container at the specified index while preserving its world transform. This ensures that the visual position and rotation of the child remain the same even when changing parents.

      Type Parameters

      • U

      Parameters

      • child: U

        The child to reparent

      • index: number

        The index to reparent the child to

      Returns U

      The reparented child

      // Basic index-specific reparenting
      const sprite = new Sprite(texture);
      oldContainer.addChild(sprite);
      // Move to new parent at index 0 (front)
      newContainer.reparentChildAt(sprite, 0);

      If index is out of bounds

    • Replace a child in the container with a new child. Copying the local transform from the old child to the new one.

      Type Parameters

      • U
      • T

      Parameters

      • oldChild: U

        The child to replace.

      • newChild: T

        The new child to add.

      Returns void

    • Changes the position of an existing child in the container.

      Parameters

      • child: C | IRenderLayer

        The child Container instance to reposition

      • index: number

        The resulting index number for the child

      Returns void

      // Basic index change
      container.setChildIndex(sprite, 0); // Move to front
      container.setChildIndex(sprite, container.children.length - 1); // Move to back

      // With error handling
      try {
      container.setChildIndex(sprite, 5);
      } catch (e) {
      console.warn('Invalid index or child not found');
      }

      If index is out of bounds

      If child is not in container

    • Swaps the position of 2 Containers within this container.

      Type Parameters

      • U

      Parameters

      • child: U

        First container to swap

      • child2: U

        Second container to swap

      Returns void

      // Basic swap
      container.swapChildren(sprite1, sprite2);

      // With error handling
      try {
      container.swapChildren(sprite1, sprite2);
      } catch (e) {
      console.warn('One or both children not found in container');
      }
      • Updates render groups
      • No effect if same child
      • Triggers container changes
      • Common in z-ordering

      If either child is not in container