【发布时间】:2015-01-07 09:46:12
【问题描述】:
我有两个 ImageView,我想知道是否有任何方法可以编写某种 if 语句来检查两个图像是否“相互碰撞”,谢谢。
【问题讨论】:
-
这是否意味着 ImageView(s) 正在运动或可以通过输入移动?
标签: android image collision-detection collision
我有两个 ImageView,我想知道是否有任何方法可以编写某种 if 语句来检查两个图像是否“相互碰撞”,谢谢。
【问题讨论】:
标签: android image collision-detection collision
你可以通过制作两类玩家来做到这一点
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();
}
}
【讨论】:
试试这个代码示例
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
}
【讨论】: