PositionComponent 之类的 Flame 游戏对象可以 Collidable 和 Draggable

Can a Flame game object such as PositionComponent be Collidable and Draggable

已成功实现 Draggable 组件。将 HitboxCollidable mixins 添加到由 PositionComponent 扩展的 class 时,拖动功能停止工作。

是否可以有一个可碰撞的可拖动组件?

颤动版本:2.2.3
Flame版本:1.0.0-releasecandidate.13

main.dart

import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'DraggablesGame.dart';

void main() {
  runApp(
    GameWidget(
      game: DraggablesGame(),
    ),
  );
}

DraggablesGame.dart

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'DraggableSquare.dart';

class DraggablesGame extends BaseGame with HasDraggableComponents, HasCollidables {

  @override
  Future<void> onLoad() async {
    add(DraggableSquare());
    add(DraggableSquare()..y = 350);
  }
}

DraggableSquare.dart

import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flame/gestures.dart';
import 'package:flutter/material.dart' show Colors;
import 'DraggablesGame.dart';

class DraggableSquare extends PositionComponent
    with Draggable, HasGameRef<DraggablesGame>, Hitbox, Collidable {
  @override
  bool debugMode = true;

  DraggableSquare({Vector2? position})
      : super(
    position: position ?? Vector2.all(100),
    size: Vector2.all(100),
  );

  Vector2? dragDeltaPosition;
  bool get isDragging => dragDeltaPosition != null;

  @override
  void update(double dt) {
    super.update(dt);
    debugColor = isDragging ? Colors.greenAccent : Colors.purple;
  }

  @override
  bool onDragStart(int pointerId, DragStartInfo info) {
    dragDeltaPosition = info.eventPosition.game - position;
    return false;
  }

  @override
  bool onDragUpdate(int pointerId, DragUpdateInfo event) {
    final dragDeltaPosition = this.dragDeltaPosition;
    if (dragDeltaPosition == null) {
      return false;
    }

    position.setFrom(event.eventPosition.game - dragDeltaPosition);
    return false;
  }

  @override
  bool onDragEnd(int pointerId, _) {
    dragDeltaPosition = null;
    return false;
  }

  @override
  bool onDragCancel(int pointerId) {
    dragDeltaPosition = null;
    return false;
  }
}

根据回答更新

Spydon 的回答建议使用 addHitbox(HitboxRectangle());。这导致了以下错误:

The method 'addHitbox' isn't defined for the type 'DraggableSquare'.

相反,此修改后的构造函数允许拖动和碰撞。

更新了 DraggableSquare 构造函数

DraggableSquare({Vector2? position})
      : super(
    position: position,
    size: Vector2.all(100),
  ) {
    final hitBox = HitboxRectangle();
    addShape(hitBox);
  }

当你添加 Hitbox mixin 时,你还必须添加一些命中框,否则它将无法知道它应该算作“命中”。 最简单的解决方案是添加一个 HitboxRectangleHitboxCircle 类型的空 hitbox。如果您没有在其中定义任何更具体的内容,这些 Hitbox 将是组件的完整大小。

所以要添加一个 HitboxRectangle,您将构造函数修改为:

  DraggableSquare({Vector2? position})
      : super(
          position: position ?? Vector2.all(100),
          size: Vector2.all(100),
        ) {
    addShape(HitboxRectangle());
  }

如果您设置 debugMode = true,您将能够直观地看到您添加的碰撞框。