addChild method
Prepares and registers one component to be added on the next game tick.
This allows you to provide a specific gameRef if this component is being added from within another component that is already on a FlameGame. You can await for the onLoad function, if present. This method can be considered sync for all intents and purposes if no onLoad is provided by the component.
Implementation
Future<void> addChild(Component component) async {
component.prepare(parent);
if (!component.isPrepared) {
// Since the components won't be added until a proper game is added
// further up in the tree we can add them to the _addLater list and
// then re-add them once there is a proper root.
_addLater.add(component);
return;
}
// [Component.onLoad] (if it is defined) should only run the first time that
// a component is added to a parent.
if (!component.isLoaded) {
final onLoad = component.onLoadCache;
if (onLoad != null) {
await onLoad;
}
component.isLoaded = true;
}
// Should run every time the component gets a new parent, including its
// first parent.
component.onMount();
if (component.children.isNotEmpty) {
await component.reAddChildren();
}
_addLater.add(component);
}