0

MonoGame フレームワークを使用している WP8 プロジェクトがあります。水平方向および垂直方向のドラッグ イベントを認識してアクションを実行するコードがいくつかありますが、これらのイベントを取得できないようです。FreeDrag ジェスチャは取得できますが、デルタは常に NaN です。

次のように、ゲームの Initialize メソッドで TouchPanel.EnabledGestures を初期化します。

  protected override void Initialize()
  {
     // TODO: Add your initialization logic here
     base.Initialize();

     TouchPanel.EnabledGestures = GestureType.HorizontalDrag | GestureType.FreeDrag;
  }

次のようにジェスチャの種類をチェックするメソッドがあります。

  private void CheckUserGesture()
  {
     while (TouchPanel.IsGestureAvailable)
     {
        var gesture = TouchPanel.ReadGesture();

        switch(gesture.GestureType)
        {
           case GestureType.DragComplete:
              System.Diagnostics.Debug.WriteLine("Drag Complete");
              break;

           case GestureType.FreeDrag:
              System.Diagnostics.Debug.WriteLine("Drag Complete");
              break;

           case GestureType.HorizontalDrag:
              if (gesture.Delta.X < 0)
                 gameVm.MoveLeft(Math.Abs((int)gesture.Delta.X));
              if (gesture.Delta.X > 0)
                 gameVm.MoveRight((int)gesture.Delta.X);

              break;

           case GestureType.VerticalDrag:
              if (gesture.Delta.Y > 0)
                 gameVm.MoveDown(Math.Abs((int)gesture.Delta.Y));
              break;

           case GestureType.Tap:
              System.Diagnostics.Debug.WriteLine("Rotating Shape Due To Tap Command");
              gameVm.RotateClockwise();
              break;
        }
     }
  }

そして、これは Update メソッドで呼び出されます。

  protected override void Update(GameTime gameTime)
  {
     base.Update(gameTime);

     // TODO: Add your update logic here
     //CheckTouchGesture();
     CheckUserGesture();
     gameVm.UpdateGame((int)gameTime.ElapsedGameTime.TotalMilliseconds);
  }

私もTouchStateを使ってみました:

  private void CheckTouchGesture()
  {
     var touchCol = TouchPanel.GetState();

     foreach (var touch in touchCol)
     {
        // You're looking for when they finish a drag, so only check
        // released touches.
        if (touch.State != TouchLocationState.Released)
           continue;

        TouchLocation prevLoc;

        // Sometimes TryGetPreviousLocation can fail. Bail out early if this happened
        // or if the last state didn't move
        if (!touch.TryGetPreviousLocation(out prevLoc) || prevLoc.State != TouchLocationState.Moved)
           continue;

        // get your delta
        var delta = touch.Position - prevLoc.Position;

        // Usually you don't want to do something if the user drags 1 pixel.
        if (delta.LengthSquared() < DragTolerence)
           continue;

        if (delta.X < 0)
           gameVm.MoveLeft(Math.Abs((int)delta.X));
        else if (delta.X > 0)
           gameVm.MoveRight((int)delta.X);
        else if (delta.Y > 0)
           gameVm.MoveDown((int)delta.Y);
     }
  }

しかし、デルタは常に NaN です。

初期化する必要があるかもしれない何かが欠けていますか?EnabledGestures タイプのさまざまな組み合わせを試しましたが、まだドラッグ イベントを機能させることができません。フリックも効かない。

ただし、タップなどは問題ありません。

ありがとう

4

1 に答える 1