0
\$\begingroup\$

I want to determine if a horizontal drag was performed from left to right or from right to left. In addition, I want to do the same with the vertical drag.

How can I do that?

How can I check if the horizontal drag was performed from left to right or from right to left?

protected override void Update(GameTime gameTime) 
    {        
        while (TouchPanel.IsGestureAvailable) 
        { 
            GestureSample gs = TouchPanel.ReadGesture(); 
            switch (gs.GestureType) 
            { 
                case GestureType.HorizontalDrag: 
                //from left to right
                Count += 1;
                //from right to left
                Count -= 1;
                break; 
                case GestureType.VerticalDrag: 
                //downwards
                Count += 1;
                //upwards
                Count -= 1;
                break; 
            } 
        } 
        base.Update(gameTime); 
    }
\$\endgroup\$
2
\$\begingroup\$

Mark the starting position of the drag. Then compare against the end position. The difference between them will tell you the direction.

With XNA, this is provided for you in the form of GestureSample.delta

The delta is a Vector2 that contains the difference for each axis. For example:

If you moved up and to the right, your delta vector might look like (10, 10). If you moved down and to the right it could look like: (14, -9).

So, the easy way to tell the direction of the swipe is:

if(gs.delta.x > 0)
  //moved right
else if(gs.delta.x < 0)
  //moved left
else
  //didn't move left or right

if(gs.delta.y > 0)
  //moved up
else if(gs.delta.y < 0)
  //moved down
else
  //didn't move up or down
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.