繁体   English   中英

如何检查wp7 XNA touch事件是否在定义的矩形内

[英]How to check if wp7 XNA touch event is within a defined rectangle

我想检查用户单击是否在我定义的一组矩形范围内,但是我应该以另一种方式执行此检查吗?

这就是我现在所拥有的,但是我不确定如何将矩形对象与touches对象进行比较

private bool CheckEnemyClicked(Vector2 vector2, out Enemy enemyOut)
{
    TouchCollection touches = TouchPanel.GetState();

    foreach (Enemy enemy in enemies)
    {
        //Find the rectangle of the sprite
        Rectangle rectangle = new Rectangle(
                                  (int)enemy.Position.X, 
                                  (int)enemy.Position.Y, 
                                  enemy.Width, 
                                  enemy.Height);

        //Check if click is hitting enemy
        if(touchinput is within rectangle)
        {
            enemyOut = enemy;
            return true;
        }
    }
    enemyOut = null;
    return false;
}

TouchCollection返回具有“ Position”成员的TouchLocation对象的集合。 矩形具有一个包含方法,该方法具有一个重载,可以检查其中是否包含特定点。 因此,XNA框架为您完成了所有艰苦的工作。

对于Touch的冲突检查部分,您的代码看起来像这样。

//Check if click is hitting enemy
foreach (TouchLocation location in collection)
{
     if (rectangle.Contains((int)location.Position.X, (int)location.Position.Y))
     {
          enemyOut = enemy;
          return true;
     }
}

TouchCollection是触摸点的集合(因为用户可能在多个位置触摸屏幕)。 您应该检查TouchCollection的各个接触点,并找到它们相对于敌人矩形的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM