replaceChildAt method

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

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.


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