将图像保留在前景上并管理碰撞

时间:2013-10-15 09:21:40

标签: c# xaml windows-phone-7 windows-phone-8

我正在开发一款适用于Windows手机的游戏应用。 我正在屏幕上移动图像(这是一张地图)。用户可以将球放在他想要的地方。我的问题是如何才能将球放在图像上?

我认为我可以检查碰撞是否在必要时将球移出矩形。

我有3种移动对象的方法(ManipulationStartedOnManipulationDeltaManipulationOver)。

我遇到的问题是:

private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
    Point A = new Point();
    Point B = new Point();

    e.Handled = true;
    var transform = (sender as UIElement).RenderTransform as TranslateTransform;
    A.X = transform.X += e.DeltaManipulation.Translation.X;
    A.Y = transform.Y += e.DeltaManipulation.Translation.Y;
    B.X = savx = transform.X;
    B.Y = savy = transform.Y;

    for (int i = 0; i < cur.lobst.Count(); i++)
    {
        if ((A.X > cur.lobst[i].px
                 && A.X < (cur.lobst[i].px + cur.lobst[i].pw))
             && (A.Y > cur.lobst[i].py
                 && A.Y < (cur.lobst[i].py + cur.lobst[i].ph)))
        {
            MessageBox.Show(
                "Point inside the rectangle, so inside (under) my object ");
        }

    }
}

永远不会显示消息框。那是为什么?

1 个答案:

答案 0 :(得分:0)

你可以试试这个......

public bool Intersects(Rect r1,Rect r2)
{
  r1.Intersect(r2);

  if(r1.IsEmpty)
  {
    return false;
  }
  else 
  {
    return true;
  }
}

然后检查...

    ismoveallowed = true;
    for (int i = 0; i < cur.lobst.Count(); i++)
    {
        if(Intersects(r1,r2))
        {
           ismovallowed = false;
           break;
        }
    }

    if(ismovealoowed)
    {
       // move the object.
    }

我希望这会对你有帮助..