0

I'm trying to collide my ball1 to greengreen drawable. how can I determine the collision?

  public boolean CheckCollisionred(View ballr,View rr_1,View rg_3) {
    Rect R1 = new Rect(ballr.getLeft(), ballr.getTop(), ballr.getRight(), ballr.getBottom());
    Rect R2 = new Rect(rr_1.getLeft(), rr_1.getTop(), rr_1.getRight(), rr_1.getBottom());
    Rect R3 = new Rect(rg_3.getLeft(), rg_3.getTop(), rg_3.getRight(), rg_3.getBottom());
    return R1.setIntersect(R2, R3);
 
}

@Override
public void run() {startballanimation();
    if (CheckCollisionred()) {
        Score++;
    }else
        Score--;
}

UPDATE: this is showing me error "Checkcollisionred() in main activity cannot be applied to expected parameters, Actual arguments"

edit: the problem is I have an image button that displays the drawables that I want to collide .

  • Use the answers to the linked question to find the location of the Drawables then use some `if` statements to detect collisions. – Code-Apprentice Jan 18 '18 at 18:17
  • You should learn about libraries which are intended for games like this. LibGDX is a very popular one. – Code-Apprentice Jan 18 '18 at 18:18
  • Please explain in more detail about what you want your app to do and what it is currently doing. Do the drawables move? Do you want them to? Feel free to post some screenshots if that will help. – Code-Apprentice Jan 18 '18 at 19:35
  • so there are three image buttons in place at the bottom of the screen and there are balls going down from the top. the drawables are not moving.when the designated drawable for the ball gets tapped,the score will add...so basically the drawable will catch the balls.. @Code-Apprentice – francis Escolar Jan 18 '18 at 20:33
  • Okay, let's simplify this. It sounds like the falling balls have nothing to do with your current question. Rather, you want to move some object on the screen as the user taps or drags on the screen. Is that correct? If so, create a [mcve] which attempts to do this. This code example should not have any balls, just the object that you are trying to move. – Code-Apprentice Jan 18 '18 at 21:08
  • If I misunderstand your question, the same advice applies: break it down to smaller, simpler pieces. Pick one piece and figure out how to do it. Rinse and repeat. – Code-Apprentice Jan 18 '18 at 21:09
  • if(the ball collides with the drawables){ score++}else show game over that's basically it @Code-Apprentice – francis Escolar Jan 18 '18 at 21:31
  • i'll keep that noted – francis Escolar Jan 18 '18 at 21:34
  • "if(the ball collides with the drawables){ score++}" Is not valid Java. You seem to have several questions. From the original question, you are asking how to do "the ball collides with the drawables". This leads back to my original comment: First get the location and size of a ball and of each drawable. Then determine if they collide. – Code-Apprentice Jan 18 '18 at 22:05
  • I suggest you simplify this to a single ball and a single drawable. – Code-Apprentice Jan 18 '18 at 22:06
  • that's just an example for you to understand. you said that I should get the "location and size of the ball" I clearly don't know how to do that.that's why i'm asking. alright I simplify. – francis Escolar Jan 18 '18 at 22:17
  • The ball is an `ImageView`. Read the link in my very first post to find how to get the location and size of any `View`. – Code-Apprentice Jan 18 '18 at 22:21
  • thanks,it's a bit clear now I should use view.getLocationOnScreen for the ball how about for the drawable? and how can I determine if they collide? should I use .intersects? – francis Escolar Jan 18 '18 at 22:34
  • Which one? You have 16 of them in your code. It will help tremendously if you follow the instructions in my previous comment: create a [mcve]. (Hint: click on this link and read it.) – Code-Apprentice Jan 18 '18 at 22:45
  • You are not refering the objects as parameter to the method, you declared 3 and called with 0 – Marcos Vasconcelos Jan 19 '18 at 18:27
  • I don't get it.. can u elaborate? @MarcosVasconcelos – francis Escolar Jan 19 '18 at 21:03
  • If you have this: (View ballr,View rr_1,View rg_3) you cant call as () you need tree parameters – Marcos Vasconcelos Jan 22 '18 at 12:40

1 Answers1

2

You can check This for Finding Collision of Two Views. You can add that drawable in any Imageview.

public boolean CheckCollision(View v1,View v2) {
    Rect R1=new Rect(v1.getLeft(), v1.getTop(), v1.getRight(), v1.getBottom());
    Rect R2=new Rect(v2.getLeft(), v2.getTop(), v2.getRight(), v2.getBottom());
    return R1.intersect(R2);
}

You can Add drawable as src

    <ImageView
android:id="@+id/imgLogo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/logo"/>
Chandan kushwaha
  • 941
  • 6
  • 26