首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在颤振中用火焰探测屏幕边界?

如何在颤振中用火焰探测屏幕边界?
EN

Stack Overflow用户
提问于 2021-12-14 07:35:36
回答 1查看 474关注 0票数 0

我想在火焰颤振中检测出一个物体何时离开屏幕。我认为有两种方法可以实现这一点,要么使用Collidable mixin,要么使用Forge2D。如果可能的话,两个人都要解释一下。

火焰版本:火焰:1.0.0-释放

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-14 11:19:02

为此使用Forge2D太过分了(这也会使许多其他事情复杂化)。但是您可以使用内置的冲突检测系统,或者可以在更新循环中检查它是否在屏幕内(这将是最有效的)。

通过使用碰撞检测系统,我们可以使用内置的ScreenCollidable,您可以这样做:

代码语言:javascript
复制
class ExampleGame extends FlameGame with HasCollidables {
  ...
  @override
  Future<void> onLoad() async {
    await super.onLoad();
    add(ScreenCollidable());
  }
}

class YourComponent extends PositionComponent with HasHitboxes, Collidable {
  @override
  Future<void> onLoad() async {
    await super.onLoad();
    // Change this if you want the components to collide with each other
    // and not only the screen.
    collidableType = CollidableType.passive;
    addHitbox(HitboxRectangle());
  }

  // Do note that this doesn't work if the component starts
  // to go outside of the screen but then comes back.
  @override
  void onCollisionEnd(Collidable other) {
    if (other is ScreenCollidable) {
      removeFromParent();
    }
  }
}

通过在update-loop中计算它:

代码语言:javascript
复制
class YourComponent extends PositionComponent with HasGameRef {
  @override
  void update(double dt) {
    final topLeft = absoluteTopLeftPosition;
    final gameSize = gameRef.size;
    if(topLeft.x > gameSize.x || topLeft.y > gameSize.y) {
      removeFromParent();
      return;
    }
    final bottomRight = absolutePositionOfAnchor(Anchor.bottomRight);
    if(bottomRight.x < 0 || bottomRight.y < 0) {
      removeFromParent();
      return;
    }
  }
}

我还建议您在Flame1.0.0发布后立即更新它。:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70345248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档