Android Rect intersetcs 方法(检查碰撞)

Android Rect intersetcs method (to check collision)

我正在尝试获取 2 个矩形来检查碰撞,但我无法成功,我什至将它们创建在彼此之上但没有成功。

int testrectX = 0 ;
public Rect testRect = new Rect(testrectX, 500, testrectX +1, 400);
public Rect testRect2 = new Rect(testrectX, 500, testrectX +1, 400);


    if (Rect.intersects(testRect, testRect2)) {

        System.out.println("testRect intersects with testRect1");

}

这是您使用的构造函数的一些文档。

public Rect (int left, int top, int right, int bottom)

Create a new rectangle with the specified coordinates. Note: no range checking is performed, so the caller must ensure that left <= right and top <= bottom.

正如 eduyayo 评论的那样,您创建了顶部 > 底部的矩形,我相信 Rect.intersects 将它们识别为具有空交叉点的空矩形。尝试确保矩形构造为非空。

谢谢大家,我错过了这个

通过使顶部小于底部来修复

public Rect testRect = new Rect(testrectX, 300, testrectX +1, 400);
public Rect testRect2 = new Rect(testrectX, 300, testrectX +1, 400);