使用Horizo​​ntalDrag和VerticalDrag XNA

时间:2013-02-01 16:51:44

标签: c# xna gesture

我目前正在VS2012上使用C#/ XNA构建游戏,在我的代码中,我希望用户触摸一个对象并在切换到垂直或水平手势时垂直和水平拖动它而不会抬起手指。以下是我的代码示例:

protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // Move the sprite by speed, scaled by elapsed time. 
        //shipPosition += shipSpeed; 

        // check whether gestures are available
        while (TouchPanel.IsGestureAvailable)
        {
            // read the next gesture
            GestureSample gesture = TouchPanel.ReadGesture();

            // has the user tapped the screen?
            switch (gesture.GestureType)
            {
                case GestureType.HorizontalDrag:

                    shipPosition += gesture.Delta;

                    break;
                case GestureType.VerticalDrag:

                    shipPosition += gesture.Delta;

                    break;
            }
        }

        // Make sure that the ship does not go out of bounds 
        shipPosition.X = MathHelper.Clamp(shipPosition.X, 35 / 2 , 765 + 35 / 2 - shipTexture.Width);
        shipPosition.Y = MathHelper.Clamp(shipPosition.Y, 21 / 2, 451 + 21 / 2 - shipTexture.Height);

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

有人可以提供建议吗?我看到某个地方我必须使用原始输入触摸,但我想知道它如何与拖动工作。

1 个答案:

答案 0 :(得分:0)

听起来你需要使用Touch Location

http://msdn.microsoft.com/en-us/library/ff434208.aspx

查看

  

从触控输入设备获取多点触控数据