Search code examples
flutterflame

Flutter flame how to add a name tag on a component


Is it possible with flame components to attach them to other components so that their position is updated when the "parent" component moves?

I want to have a component and attached to a sprite that contains a name tag with an xy-offset.


Solution

  • This is the basic functionality of what we call FCS (Flame Component System).

    All components can be added to other components with the add method and then they will get the same transforms as their ancestors before their own transform is applied.

    Example:

    final parent = PositionComponent(position: Vector2(100, 100));
    final child = PositionComponent(position: Vector2(10, 10));
    parent.add(child);
    // It will be properly added to the parent before the next tick.
    print(child.absolutePosition); // Vector2(110, 110);
    

    Do note that if you look at the position variable it will always report the local position.