0

i have created a rectangle

public Rectangle rectangle_hitbox;

It has the coordinates and dimensions of the enemies. In the render method, I want that when this rectangle is pressed, the string "Done" is printed. I tried with:

if(Gdx.input.isTouched()){
   System.out.println("Done");
} else {
   System.out.println("Missed");
}

But it works anywhere on the map and not only in rectangles

witsel
  • 17
  • 6

1 Answers1

0

The Gdx.input.isTouched() method doesn't know about the position of your rectagle. It just check whether the screen is touched or not.

Try using Gdx.input.getX() and Gdx.input.getY() to get the coordinates of the click or touch event when the screen is touched and check these coordinates against the coordinates of your rectangle.

Also don't forget to unproject the coordinates if you are using a camera. See this answer for more information.

if(Gdx.input.isTouched()){
    Vector3 touchPosition = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0f);
    // TODO maybe unproject the position using camera.unproject(touchPosition);
    if (isTouchInsideRectangle(touchPosition)) {
        System.out.println("Done");
    }
    else {
        System.out.println("Missed");
    }
}
Tobias
  • 2,547
  • 3
  • 14
  • 29