flutter - 如何检测带有 flutter/火焰的游戏中的滑动

标签 flutter dart flame

我想制作一款火热的游戏。对于这个游戏,我想检测滑动。

我可以在教程的帮助下实现点击识别。但是我无法通过滑动检测来实现它。

我的主要 Taprecognition 看起来像这样: 我的主要功能是

void main() async{
  Util flameUtil = Util();
  await flameUtil.fullScreen();
  await flameUtil.setOrientation(DeviceOrientation.portraitUp);

  GameManager game = GameManager();
  runApp(game.widget);

  TapGestureRecognizer tapper = TapGestureRecognizer();
  tapper.onTapDown = game.onTapDown;
  flameUtil.addGestureRecognizer(tapper);
}

在我的 GameManager 类中我有:

class GameMAnager extends Game{
  // a few methods like update, render and constructor
  void onTapDown(TapDownDetails d) {
    if (bgRect.contains(d.globalPosition)) { //bgRect is the background rectangle, so the tap works on the whole screen
      player.onTapDown();
    }
  }

我的播放器类包含:

  void onTapDown(){
    rotate();
  }

现在我想将其更改为沿滑动方向而不是 onTapDown 旋转。 我试图以某种方式添加

  GestureDetector swiper = GestureDetector();
  swiper.onPanUpdate = game.onPanUpdate;

到我的主要和

  void onPanUpdate() {

  }

到我的 gameManager 类。但是我找不到任何类似于 TapDownDetails 的平移。

对此有什么建议吗?

我看到一些帮助将小部件包装在 GestureDetector 中并像这样使用它:

GestureDetector(onPanUpdate: (details) {
  if (details.delta.dx > 0) {
    // swiping in right direction
  }
});

但我无法让它在我的项目中发挥作用。

最佳答案

您可以使用 Horizo​​ntalDragGestureDetector(如果您需要两个轴,则可以使用 PanGestureRecognizer) 在您的主要方法中使用以下内容

HorizontalDragGestureRecognizer tapper = HorizontalDragGestureRecognizer();
tapper.onUpdate = game.dragUpdate;

然后在您的 GameManager 中执行以下操作

void dragUpdate(DragUpdateDetails d) {
    // using d.delta you can then track the movement and implement your rotation updade here
}

这应该可以解决问题 :D

关于flutter - 如何检测带有 flutter/火焰的游戏中的滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578343/

相关文章:

android - 使用-sdk :minSdkVersion 16 cannot be smaller than version 23 declared in library [:audioplayers]

flutter - 使用 Flutters 应用内购买插件检查订阅有效性?

firebase - 使用Flutter在Firebase中使用Stream <QuerySnapshot>检索数据

Flutter - PageView animateToPage 将加载中间页面。如何避免?

reflection - 基于镜子的反射和传统反射有什么区别?

flutter - Dart const 作为预处理器指令

animation - 对容器窗口小部件进行动画处理,使屏幕向左移动

flutter - 如何处理平台游戏的对象碰撞?

flutter 火焰 : How to calculate the relect of a raycast?

flutter - 检测用户何时停止触摸屏幕