首页 > 解决方案 > 如何检测带有颤动/火焰的游戏中的滑动

问题描述

我想创造一个充满火焰的游戏。对于这个游戏,我想检测滑动。

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

我使用 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
  }
});

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

标签: flutterdartflame

解决方案


您可以使用 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


推荐阅读