MonoGame - 拖放

时间:2014-08-10 10:01:08

标签: monogame

我目前正在使用monogame框架编写游戏。我无法正确对触摸输入作出反应。我希望用户能够拖动“objToDrag”。问题是deltaX和deltaY都是零。这是我的代码:

var touchState = TouchPanel.GetState();

foreach (var touch in TouchPanel.GetState())
{
  if (touch.State == TouchLocationState.Moved)
  {
    TouchLocation prevLoc;

    if (!touch.TryGetPreviousLocation(out prevLoc)) continue;

    float deltaX = touch.Position.X - prevLoc.Position.X;
    float deltaY = touch.Position.Y - prevLoc.Position.Y;

    this.objToDrag.X += deltaX;
    this.objToDrag.Y += deltaY;
  }
}

1 个答案:

答案 0 :(得分:0)

使用delta值计算对象应移动的距离,无需计算自己的对象。

foreach (var touch in TouchPanel.GetState())
{
  if (touch.State == TouchLocationState.Moved)
  {
       TouchLocation prevLoc;

       if (!touch.TryGetPreviousLocation(out prevLoc)) continue;

       float deltaX = touch.Delta.X;
       float deltaY = touch.Delta.Y;

       this.objToDrag.X += deltaX;
       this.objToDrag.Y += deltaY;
    }
}