我有以下功能,它将告诉我 2 个精灵何时在 x 轴上重叠:
public int getSecondObjectX(int secObjWidth)
{
int rndX = random.nextInt(AndroidApplication.getInstance().device_width - secObjWidth);
Rect sObj = new Rect(rndX, 20, rndX + secObjWidth, 10);
Rect droid = new Rect(myAnimation.getXcord(), 20, myAnimation.getXcord() + myAnimation.getCurrentImage().getWidth(), 10);
Log.d("Compare Rect", "["+sObj.left+", "+sObj.right+"] ["+droid.left+", "+droid.right+"]");
if(Rect.intersects(sObj, droid))
{
Log.d("INTERSECT", "INTERSECT");
return getSecondObjectX(secObjWidth);
}
else
return rndX;
}
但我从来没有得到相交,这是 Logcat:
03-19 15:08:12.617: D/Compare Rect(631): [183, 231] [141, 189] <-- should be true
03-19 15:08:22.186: D/Compare Rect(631): [158, 206] [172, 220] <-- should be true
03-19 15:08:26.346: D/Compare Rect(631): [123, 171] [178, 226]
03-19 15:08:30.146: D/Compare Rect(631): [156, 204] [155, 203] <-- should be supertrue
03-19 15:08:32.617: D/Compare Rect(631): [84, 132] [172, 220]
有什么问题?