gpt4 book ai didi

c# - 如何检查 wp7 XNA 触摸事件是否在定义的矩形内

转载 作者:太空宇宙 更新时间:2023-11-03 19:25:13 25 4
gpt4 key购买 nike

我想检查用户点击是否在我定义的一组重新排列内,但我是否应该以另一种方式进行此检查?

这就是我现在所拥有的,但我不知道如何比较矩形对象和触摸对象

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 返回具有“位置”成员的 TouchLocation 对象的集合。 Rectangle 有一个 Contains 方法,它有一个重载,可以检查一个特定的点是否包含在其中。所以 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;
}
}

关于c# - 如何检查 wp7 XNA 触摸事件是否在定义的矩形内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9227473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com