这是我用来防止这一糟糕事故发生在我心爱的角色身上的机制:

void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
    print(
        "other is $other and intersectionPoints length is ${intersectionPoints.length}");
    if (other is Brick && intersectionPoints.length == 2) {
      isOnGround = true;
      final intersectionsVector=
          (intersectionPoints.elementAt(0) + intersectionPoints.elementAt(1)) /
              2;
      final collisionVector = absoluteCenter - intersectionsVector;
      final penetrationDistance = size.x / 2 - collisionVector.length;
      collisionVector.normalize();
      if (Vector2(0, -1).dot(collisionVector) > 0.9) {
        isOnGround = true;
      }

      position += collisionVector.scaled(penetrationDistance);
    }

    super.onCollision(intersectionPoints, other);
  }

我的大部分游戏甚至变量的灵感都来自于火焰的Ember角色, 唯一的区别是Ember游戏不使用CameraComponent.Follow(播放器),但我使用. 一切都很顺利,我的角色走得很开心,突然他穿过了地面,死了.

所以我加了这个,看看什么是什么:

  print(
        "other is $other and intersectionPoints length is ${intersectionPoints.length}");

我看到的唯一问题是交叉点的长度是1的一些倍,也许这就是我的角色倒下的时间.我也不知道为什么. 我为我的播放器使用CircleHitBox,为我的砖块(平台)使用RecanglehitBox. 那么我怎么才能更好地处理这件事呢.谢谢

推荐答案

就像你已经注意到的,intersectionPoints不一定包含2个条目,当发生这种情况时,你甚至应该得到一个错误,因为你正在做elementAt(1),并且在那个索引上可能没有AND元素.

如果你想求交叉点的平均值,我建议这样做:

final Vector2 average = Vector2.zero;
for (final point in intersectionPoints) {
  average.add(point);
}
average.scale(1/intersectionPoints.length);

Flutter相关问答推荐

Flutter:如何从Bottom Navigator中删除此内容

为什么我需要等待重新访问Firestore数据库,即使它已经做了之前?

Flutter版本3.19.2需要更新版本的Kotlin Gradle插件./android/build.gradle:ext.kotlin_version = latest-version>'

从底部开始固定,然后使其可拖曳Flutter

无法让Riverpod Stream发出新值

Wired Firebase错误-[CLOUD_FIRESTORE/UNAvailable]该服务当前不可用

在创建显示此错误的Flutter 深度链接时

从future 列表到流列表Flutter Firebase

在带有 flutter 的 Native Android 中,EventChannel.EventSink 始终为 null

无法从一个页面导航到下一个页面 (Flutter)

#Flutter/Riverpod - 使用 AsyncNotifier 时是否可以仅调用错误?

如何更改 DropDownMenu 的显示方向?

如何扫描来自给定链接或按钮的重定向中的所有 URL?

Pub get 尚未运行

仅使用 Flutter 在本地环境中托管 Web 服务器

Getx Flutter 在更新值时抛出空错误

Flutter Web App 未启动:脚本的 MIME 类型不受支持

NoSuchMethodError:方法 '[]' 在 null 上被调用.我的信息流中的错误

主题:ThemeData() Flutter

如何在flutter中共享容器和gridview之间的滚动条?