Search code examples
flutterdartflame

Multiple issues after migrating the Flame package in Flutter


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<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();
  }

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'.


Solution

  • 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<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);
      }
    }
    

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