多次迁移Flutter中的Flame包后出现的问题。

huangapple go评论58阅读模式
英文:

Multiple issues after migrating the Flame package in Flutter

问题

以下是您提供的代码部分的翻译:

我有以下这个类,但在更新包时出现了多个错误。我无法记住之前的版本是什么,但之前是正常工作的,但现在我正在使用 flame: ^1.6.0

class Monkey extends PositionComponent with HasHitboxes, Collidable, HasGameRef<MovingBackgroundGame> {
  Monkey({Vector2? position, bool isVertical = false})
      : super(
          position: position,
          size: Vector2(40, 80),
          anchor: Anchor.topLeft,
        );

  bool collided = false;
  HitboxShape hitbox = HitboxRectangle(relation: Vector2(1, 1));

  @override
  Future<void> onLoad() async {
    addHitbox(hitbox);
    super.onLoad();
  }

以下是您提到的错误:

  1. HasHitboxesCollidable - 类只能混合(mix in)混入和类。
  2. HitboxShape - 未定义类 'HitboxShape'。
  3. HitboxRectangle - 方法 'HitboxRectangle' 未定义于类型 'Monkey'。
  4. addHitbox - 方法 'addHitbox' 未定义于类型 'Monkey'。
英文:

I got the below class but having multiple errors when I update my package. I didn't manage to capture what was my previous version but it was working before but I'm using flame: ^1.6.0 now.

class Monkey extends PositionComponent with HasHitboxes, Collidable, HasGameRef&lt;MovingBackgroundGame&gt; {
  Monkey({Vector2? position, bool isVertical = false})
      : super(
          position: position,
          size: Vector2(40, 80),
          anchor: Anchor.topLeft,
        );

  bool collided = false;
  HitboxShape hitbox = HitboxRectangle(relation: Vector2(1, 1));

  @override
  Future&lt;void&gt; onLoad() async {
    addHitbox(hitbox);
    super.onLoad();
  }

These are the following errors that I got. I tried to follow the documentation but haven't seen about these errors.
HasHitboxes and Collidable - Classes can only mix in mixins and classes.
HitboxShape - Undefined class 'HitboxShape'.
HitboxRectangle - The method 'HitboxRectangle' isn't defined for the type 'Monkey'.
addHitbox - The method 'addHitbox' isn't defined for the type 'Monkey'.

答案1

得分: 1

HasHitboxesCollidable mixins不再需要,所以你可以将它们移除。如果你想在此组件内对碰撞做出反应,可以添加CollisionCallbacks

然后,HitboxRectangle已重命名为RectangleHitbox,由于你设置了关系为(1, 1),所以在构造函数中实际上不需要添加任何内容,因为(1, 1)是默认值。

addHitbox已被移除,因为现在碰撞框是普通组件。

v1.6.0中,该类将如下所示:

class Monkey extends PositionComponent with CollisionCallbacks, HasGameRef<MovingBackgroundGame> {
  Monkey({Vector2? position, bool isVertical = false})
      : super(
          position: position,
          size: Vector2(40, 80),
          anchor: Anchor.topLeft,
        );

  RectangleHitbox hitbox = RectangleHitbox();

  @override
  Future<void> onLoad() async {
    super.onLoad();
    addHitbox(hitbox);
  }
}

我猜测你之前可能在v1.0.0版本上。

英文:

The HasHitboxes and Collidable mixins are no longer needed, so you can just remove those ones. You can add CollisionCallbacks if you want to react to collisions within this component though.

Then the HitboxRectangle has been renamed to RectangleHitbox and since you set the relation to (1, 1) you don't actually have to add anything at all in its constructor since (1, 1) is the default.

addHitbox is removed since hitboxes now are normal components.

This is what the class would look like in v1.6.0:

class Monkey extends PositionComponent with CollisionCallbacks, HasGameRef&lt;MovingBackgroundGame&gt; {
  Monkey({Vector2? position, bool isVertical = false})
      : super(
          position: position,
          size: Vector2(40, 80),
          anchor: Anchor.topLeft,
        );

  RectangleHitbox hitbox = RectangleHitbox();

  @override
  Future&lt;void&gt; onLoad() async {
    super.onLoad();
    addHitbox(hitbox);
  }
}

I would guess that you were on maybe v1.0.0 before you upgraded.

huangapple
  • 本文由 发表于 2023年3月12日 13:16:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定