出于某种原因,我的计划无法通过Rect.intersects(re,hitcore)
。如果我使用System.out.println()re.flattenToString()
,我会看到两个矩形相交,但它赢了return true
。
我也尝试使用re.intersect(hitcore)
,但仍然没有。帮助
import android.graphics.Rect;
public class Enemy {
public Rect re, hitCore;
private boolean hit = false;
public Enemy() {
re = new Rect(0, 0, 0, 0);
hitCore = new Rect(0, 0, 0, 0);
}
public void update() {
re.set(centerX + leftX, centerY + topY, rightX, botY); // these are set by another class
hitCore.set(ship.getCenterX() + 3, ship.getCenterY() + 10, 93, 20);
//System.out.println(re.flattenToString() + " rect1");
//System.out.println(hitCore.flattenToString() + " rect2");
checkHit(hitCore);
}
private void checkHit(Rect hitCore) {
if (Rect.intersects(hitCore, re)) {
hit = true;
}
}
}
答案 0 :(得分:0)
我明白了。我正在使用x,y和width,height设置矩形。实际参数是左上角坐标和右下角坐标。
rect.set(x1,y1,x2,y2);