getChildByName method

Node? getChildByName(
  1. String name, {
  2. bool excludeAnimationPlayers = false,
})

Searches this node's descendants for the first child whose Node.name matches name.

Performs a depth-first search of the subtree rooted at this node and returns the first match, or null if no descendant has the given name.

When excludeAnimationPlayers is true, descendants that already have an animation player attached are skipped — primarily used internally to avoid recursing into clip-attached subtrees.

Implementation

Node? getChildByName(String name, {bool excludeAnimationPlayers = false}) {
  for (var child in children) {
    if (excludeAnimationPlayers && child._animationPlayer != null) {
      continue;
    }
    if (child.name == name) {
      return child;
    }
    var result = child.getChildByName(name);
    if (result != null) {
      return result;
    }
  }
  return null;
}