【问题标题】:How to detect a swipe in a game with flutter/flame如何检测带有颤动/火焰的游戏中的滑动
【发布时间】:2019-10-27 10:04:47
【问题描述】:

我想创造一个扑腾扑腾的游戏。对于这个游戏,我想检测滑动。

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

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

但我无法让它在我的项目中运行。

【问题讨论】:

    标签: flutter dart flame


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      如果您需要两个轴,则可以使用 PanGestureRecognizer(正如@Marco Papula 所说)。

      在你的主要方法中使用它:

      PanGestureRecognizer panGestureRecognizer = PanGestureRecognizer();
      panGestureRecognizer.onEnd = game.onPanUpdate;
      flameUtil.addGestureRecognizer(panGestureRecognizer);
      

      还有你的 onPanUpdate 方法:

      void onPanUpdate(DragEndDeatils d) {
          if(d.velocity.pixelsPerSecond.dx.abs()>d.velocity.pixelsPerSecond.dy.abs()) {
            // X Axis
            snake.velocity = d.velocity.pixelsPerSecond.dx<0 ? LeftSwipe : RightSwipe;
          } else {
            // Y Axis
            snake.velocity = d.velocity.pixelsPerSecond.dy<0 ? UpSwipe : DownSwipe;
          }
      }
      

      【讨论】:

        【解决方案3】:

        如果您使用的是更新的 (v1) 版本的 Flame,则不再需要将其包装在自己的 GestureDetector 中(认为可以)。现在 Flame 已经为所有事件内置了包装器,包括平移!您可以将游戏与:

        class MyGame extends BaseGame with PanDetector {
          // ...
        }
        

        并实现onPanUpdate 以获得所需的行为;或使用无数其他检测器(查看documentation 了解更多选项和有关如何使用它的详细信息)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多