我有两个ImageViews,我想知道是否有任何方法可以编写某种if语句来检查两个图像是否相互“打”,谢谢。
最佳答案
您可以通过制作两类玩家来做到这一点
public class Player
{
int X;
int Y;
int Width;
int Height;
}
public class Enemy
{
int X;
int Y;
int Width;
int Height;
}
然后在gameloop中使用此代码
foreach (Enemy e in EnemyCollection)
{
Rectangle r = new Rectangle(e.X,e.Y,e.Width,e.Height);
Rectangle p = new Rectangle(player.X,player.Y,player.Width,player.Height);
// Assuming there is an intersect method, otherwise just handcompare the values
if (r.Intersects(p))
{
// A Collision!
// we know which enemy (e), so we can call e.DoCollision();
e.DoCollision();
}
}