user3817731 user3817731 - 1 year ago 89
Java Question

How to see if a texture is touched in libgdx?

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);;

int randomX2 = (int)MathUtils.random(100,500);
int randomY2 = (int)MathUtils.random(100,500);
game.batch.draw(boxImage, randomX2, randomY2);




Answer Source

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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download