Rect.intersects不起作用

时间:2016-01-26 17:03:14

标签: java android

我有一个RectList的ArrayList,它由x,y,x2,y2值定义,另一个Rect在屏幕上移动,应该与ArrayList中的矩形相交。但是,当矩形相交时,什么也没发生。以下是我的相关代码:

public static ArrayList<Rect> rectangles = new ArrayList<Rect>(100);

 public static ArrayList<Rect> rectangles2 = new ArrayList<Rect>(90);

public static Rect intersected = new Rect();



 int bar_x = 32;

int bar_y = 653;

 int bar_x2 = 183;

 int bar_y2 = 648;

 int BarSpeed = 2;

Rect bar;

Paint paint;



public GameScreen(Game game) {

super(game);




// Initialize game objects here




  bar = new Rect();

 bar.set(bar_x, bar_y, bar_x2, bar_y2);

rectangles.add(new Rect(135,614,160,588));

    rectangles.add(new Rect(124,541,147,512));
for (Rect rect : rectangles) {

   if(Rect.intersects(bar, rect)) {

       Log.d("GameScreen", "intersected");

    intersected = rect;

    checkButtons();




  }

}



if (BarisMoving) {




  bar_y -= BarSpeed;

  bar_y2 -=BarSpeed;

  bar.set(bar_x, bar_y, bar_x2, bar_y2);

 }

}




private void checkButtons() {

 Log.d("GameScreen","Note and Bar Intersected");



KeyButton keybutton = new KeyButton();



  if (B1Pressed == keybutton.getB1() && B2Pressed == keybutton.getB2() && B3Pressed == keybutton.getB3()) {

      Log.i("GameScreenTest", "Correct");

      score++;

  } else {

      Log.i("GameScreenTest - Result", "Wrong");

      Log.i("GameScreenTest - Button Pressed", Boolean.toString(B1Pressed));

      Log.i("GameScreenTest - Button Required", Boolean.toString(keybutton.getB1()));

  }

}

在我的LOGCAT中,没有写入“相交”。我怎样才能使矩形相交?

1 个答案:

答案 0 :(得分:0)

Rect constructor(左,上,右,下)。看起来你正在经过(左,顶,宽,高)。将这些更改为(左+宽)和(顶+高),你应该得到更好的结果。