0

按照此处的示例,是否可以检测与屏幕特定侧的碰撞,例如屏幕底部?

Flutter 版本:2.2.3
Flame 版本:1.0.0-releasecandidate.13

MyCollidable 类的 onCollision 方法

@override
  void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
    if (other is ScreenCollidable) {
      _isWallHit = true;
      // Detect which side?
      return;
    }
  }
4

1 回答 1

1

没有内置的方法,但是您可以使用游戏的大小很容易地计算出您正在与哪一侧发生碰撞并将碰撞点转换为屏幕坐标(如果您不更改,则无需执行此步骤缩放级别或移动相机)。

代码将是这样的:

class MyCollidable extends PositionComponent
    with Hitbox, Collidable, HasGameRef {
  
  ...

  void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
    if (other is ScreenCollidable) {
      _isWallHit = true;
      final firstPoint = intersectionPoints.first;
      // If you don't move/zoom the camera this step can be skipped
      final screenPoint = gameRef.projectVector(firstPoint);
      final screenSize = gameRef.size;
      if (screenPoint.x == 0) {
        // Left wall (or one of the leftmost corners)
      } else if (screenPoint.y == 0) {
        // Top wall (or one of the upper corners)
      } else if (screenPoint.x == screenSize.x) {
        // Right wall (or one of the rightmost corners)
      } else if (screenPoint.y == screenSize.y) {
        // Bottom wall (or one of the bottom corners)
      }
      return;
    }
  }
于 2021-09-06T09:39:25.353 回答