David Elliott
David Elliott

Reputation: 113

Collision detection not working in Android

This is the code that I have to detect when two ImageViews collide in Android:

Rect rc1 = new Rect();
brickimg.getDrawingRect(rc1);
Rect rc2 = new Rect();
playerimage.getDrawingRect(rc2);
if (Rect.intersects(rc1, rc2)) {
// intersection is detected
// here is your method call
  button.setText("Shrekt m9"); 
}
Rect rc3 = new Rect();
brickimg2.getDrawingRect(rc3);
Rect rc4 = new Rect();
playerimage2.getDrawingRect(rc3);
if (Rect.intersects(rc3, rc4)) {
// intersection is detected
// here is your method call
  button.setText("this works"); 
 }
if(Rect.intersects(rc2, rc3)){
 button.setText("this works too"); 
 }

When I run the app, it doesn't detect anything. Am I doing something wrong?

Upvotes: 0

Views: 577

Answers (1)

bumbumpaw
bumbumpaw

Reputation: 2530

try like this :

change your Rect as the one of your created rectangle object.

if (rc1.intersects( rc2)) {
// intersection is detected
// here is your method call
  button.setText("Shrekt m9"); 
}
Rect rc3 = new Rect();
brickimg2.getDrawingRect(rc3);
Rect rc4 = new Rect();
playerimage2.getDrawingRect(rc3);
if (rc3.intersects( rc4)) {
// intersection is detected
// here is your method call
  button.setText("this works"); 
 }
if(rc2.intersects( rc3)){
 button.setText("this works too"); 
 }

Upvotes: 1

Related Questions