I'm trying to make sprites act as buttons, so when a sprite is pressed, it performs an action. This is my code
if(Gdx.input.justTouched())
{
int x = Gdx.input.getX();
int y = Gdx.input.getY();
spriteIterator = spriteArray.iterator();
while(spriteIterator.hasNext()){
Sprite cur = spriteIterator.next();
if(cur.getBoundingRectangle().contains(x, y)) {
System.out.println("Pressed button");
//Change to collide message, and pause the game here
}
}
}
It doesn't seem to work. Button press does register but in seemingly random places and times. What's the better way to go about this please?