How to see if a texture is touched in libgdx?

Tags: , ,

pretty much i want my texture to move to a random position on the screen when its touched and when its missed i want to system.out(“missed”). I cant figure out how to see if its touched. right now i can only get if the screen is touch and it records about 10 touches for every one touch because it renders so fast.

public void render(float delta) {,1,0,1);;   


    if(Gdx.input.isTouched()) {
        int randomX2 = (int)MathUtils.random(100,500);
        int randomY2 = (int)MathUtils.random(100,500);
        game.batch.draw(boxImage, randomX2, randomY2);


If randomX2 and randomY2 are your coordinates for the texture, you can check with this code:

Rectangle bounds = new Rectangle(randomX2, randomY2, boxImage.getWidth(), boxImage.getHeight());
Vector3 tmp = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

if (bounds.contains(tmp.x, tmp.y)) {
    System.out.println("Is touched");

Source: stackoverflow