2012-08-02 107 views
0

在我的游戏中,我有矩形ImageView's,从屏幕顶部掉下来。在底部还有另一个可以与加速度计一起移动的ImageViewImageView碰撞

现在我想检查一下,从顶部落下的ImageView是否与底部的ImageView发生碰撞。我怎样才能做到这一点?

+0

通过在surfaceview上绘制位图来做这样的事情不是更好的办法吗?然后碰撞检测很容易。 – 2012-08-02 12:21:06

+0

sry我不知道这件事。你可以请给我链接教程吗? – Stupe 2012-08-02 12:23:39

+0

这里是一个:http://www.droidnova.com/playing-with-graphics-in-android-part-ii160.html虽然只是谷歌“android surfaceview教程”,你会发现一百万其他人。 Youtube视频太 – 2012-08-02 12:26:41

回答

0

我不太确定你可以碰撞,但仍然可以使用getMeasuredWidth()和Height()来确定你的方形边界框。然后看看两个正方形的边缘是否在相对的边界框中...

if((square1.x > square2.x && square1.x < square2.x+square2.width) && (square1.y > square2.y && square1.y < square2.y+square2.height)) 
//collision happened 
+0

sry,但是这是为Android?因为值'x'和'y'不存在... – Stupe 2012-08-02 12:21:55

0

试试这个。此功能检测2个ImageViews之间的冲突

public boolean collision(ImageView a, ImageView b){ 
    float bl = a.getY(); 
    float bt = a.getX(); 
    float br = a.getWidth() + bl; 
    float bb = a.getHeight() + bt; 
    float pl = b.getY(); 
    float pt = b.getX(); 
    float pr = b.getWidth() + pl; 
    float pb = b.getHeight() + pt; 
    if (bl <= pr && bl >= pl && bt >= pt && bt <= pb) { 
     return true; 

    } else if (br >= pl && br <= pr && bb >= pt && bb <= pb) { 
     return true; 
    } else if (bt <= pb && bt >= pt && br >= pl && br <= pr) { 
     return true; 
    } else if (bb >= pt && bb <= pb && bl >= pl && bl <= pr) { 
     return true; 
    } 
    return false; 
}