I'm building a GUI for my game with LibGDX.scene2d.ui and I have a problem when I'm trying to handle inputs. I have the following code to make something happen when button is pressed but it doesn't work:
enterButton.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent e, float x, float y, int p, int b) {
Gdx.app.log("touched", "down");
return true;
}
@Override
public void touchUp(InputEvent e, float x, float y, int p, int b) {
Gdx.app.log("touched", "up");
if(x > enterButton.getX() && x < enterButton.getRight() && y > enterButton.getY() && y < enterButton.getTop()) {
Gdx.app.log("cond", "cursor on actor");
if(validate(loginField.getText(), passField.getText())) {
Gdx.app.log("cond", "validated");
openMenu();
}
}
Gdx.app.log("untouched", "up");
}
});
But practically same code work correctly:
registerButton.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent e, float x, float y, int p, int b) {
return true;
}
@Override
public void touchUp(InputEvent e, float x, float y, int p, int b) {
if(x > registerButton.getX() && x < registerButton.getRight() && y > registerButton.getY() && y < registerButton.getTop()) {
registerWindow.setVisible(true);
baseWindow.setVisible(false);
}
}
});
After single click on the enterButton I'm getting this output made with LibGDX logging:
touched: down
touched: up
untouched: up
What can be wrong?
You can try using a ClickListener to check if the touch is within bounds or not, like so:
enterButton.addListener(new ClickListener() {
@Override
public void touchDown(InputEvent event, float x, float y, int pointer, int button) {
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
if (isOver()) {
if(validate(loginField.getText(), passField.getText())) {
openMenu();
}
}
}
});
You can also try setting the debug flag on the Actor so that you can see where the click bounds actually are by using enterButton.debug();