2014-03-25 42 views
0

由於某種原因,我的程序無法通過Rect.intersects(re,hitcore)。如果我使用System.out.println()re.flattenToString(),我看到兩個矩形相交,但不會出現return true無法獲取Rect.intersects(rec,rec2)工作

我也試過使用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

我想通了。我使用x,y和寬度,高度來設置矩形。實際參數是左上角座標和右下角座標。

rect.set(x1,y1,x2,y2);