附加组件以便它们一起移动

Attaching components so they move together

是否可以将火焰组件附加到其他组件,以便在“父”组件移动时更新它们的位置?

我想要一个 Hitbox 组件,并使用 xy 偏移将玩家精灵、名称标签等附加到它。

您可以使用任何 PositionComponent(Flame 中的大多数组件继承自 PositionComponent)并向其添加子项。

例如,我们创建一个简单的正方形,它有两个正方形子项,它们与父项一起移动、缩放和旋转:

class Square extends PositionComponent {
  Square(Vector2 position, Vector2 size, {double angle = 0})
      : super(
          position: position,
          size: size,
          angle: angle,
        );
}

class ParentSquare extends Square with HasGameRef {
  ParentSquare(Vector2 position, Vector2 size) : super(position, size);

  @override
  Future<void> onLoad() async {
    super.onLoad();
    // All positions here are in relation to the parent's position
    add(Square(Vector2(100, 100), Vector2(50, 50), angle: 2));
    add(Square(Vector2(160, 100), Vector2(50, 50), angle: 3));
  }
}

可以看一个例子here。 (如果您按 <>,您将看到该示例的代码)