【问题标题】:How to detect when two images "collide"?如何检测两个图像何时“碰撞”?
【发布时间】:2015-01-07 09:46:12
【问题描述】:

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

【问题讨论】:

  • 这是否意味着 ImageView(s) 正在运动或可以通过输入移动?

标签: android image collision-detection collision


【解决方案1】:

你可以通过制作两类玩家来做到这一点

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

【讨论】:

  • 有没有更简单的方法来检查它们是否碰撞?
  • 使用图片的默认坐标
  • 只要仔细检查答案,你就会找到你的解决方案
【解决方案2】:

试试这个代码示例

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
 }

【讨论】:

  • 这似乎不适用于我在第一张图片上的动画
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-29
相关资源
最近更新 更多