Registering InputListener in libGDX
Posted
by
JPRO
on Game Development
See other posts from Game Development
or by JPRO
Published on 2013-08-01T20:49:42Z
Indexed on
2013/08/02
16:07 UTC
Read the original article
Hit count: 384
I'm just getting started with libGDX and have run into a snag registering an InputListener for a button. I've gone through many examples and this code appears correct to me but the associated callback never triggers ("touched" is not printed to console). I'm just posting the code with the abstract game screen and the implementing screen. The application starts successfully with a label of "Exit" in the bottom left hand corner, but clicking the button/label does nothing.
I'm guessing the fix is something simple. What am I overlooking?
public abstract class GameScreen<T> implements Screen
{
protected final T game;
protected final SpriteBatch batch;
protected final Stage stage;
public GameScreen(T game)
{
this.game = game;
this.batch = new SpriteBatch();
this.stage = new Stage(0, 0, true);
}
@Override
public final void render(float delta)
{
update(delta);
// Clear the screen with the given RGB color (black)
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
public abstract void update(float delta);
@Override
public void resize(int width, int height)
{
stage.setViewport(width, height, true);
}
@Override
public void show()
{
Gdx.input.setInputProcessor(stage);
}
// hide, pause, resume, dipose
}
public class ExampleScreen extends GameScreen<MyGame>
{
private TextButton exitButton;
public ExampleScreen(MyGame game)
{
super(game);
}
@Override
public void show()
{
super.show();
TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.font = Font.getFont("Origicide", 32);
buttonStyle.fontColor = Color.WHITE;
exitButton = new TextButton("Exit", buttonStyle);
exitButton.addListener(new InputListener() {
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touched");
}
});
stage.addActor(exitButton);
}
@Override
public void update(float delta)
{
}
}
© Game Development or respective owner