replaceChildAt method

  1. @override
void replaceChildAt (DisplayObject child, int index)
override

Replaces the child at the specified index position with the new child. The current child at this position is removed.

The parent property of the removed child is set to null, and the object is garbage collected if no other references to the child exist.

Implementation

@override
void replaceChildAt(DisplayObject child, int index) {
  if (index < 0 || index >= _children.length) {
    throw new ArgumentError("The supplied index is out of bounds.");
  } else if (child == this) {
    throw new ArgumentError(
        "An object cannot be added as a child of itself.");
  } else if (child.parent == this) {
    if (_children.indexOf(child) == index) return;
    throw new ArgumentError(
        "The display object is already a child of this container.");
  } else {
    child.removeFromParent();
    _throwIfAncestors(child);
    _clearChildParent(_children[index]);
    _children[index] = child;
    _setChildParent(child);
  }
}