如何检测两个图像何时发生碰撞"?

时间:2015-01-07 01:15:44

标签: android image collision-detection collision

我有两个ImageView,我想知道是否有任何方法可以编写某种if语句来检查这两个图像是否“互相撞击”,谢谢。

2 个答案:

答案 0 :(得分:1)

你可以通过制作两类球员来做到这一点

public class Player
{
    int X;
    int Y;
    int Width;
    int Height;
}

public class Enemy
{
    int X;
    int Y;
    int Width;
    int Height;
}

然后在游戏循环中使用此代码

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();
    }
}

答案 1 :(得分:0)

试试此代码示例

Rect rc_img1 = new Rect();
 imageView1.getDrawingRect(rc_img1);

 Rect rc_img2 = new Rect();
 imageView2.getDrawingRect(rc_img2);

 if (Rect.intersects(rc_img1, rc_img2) {
   // intersection is detected
   // DO WHAT YOU WANT
 }