Reputation: 1887
Im testing libgdx but I'm getting stuck in user input handling.
My first attempt was using Gdx.input directly from render method but I feel like reinventing the wheel cause I'm writing a lot of code to detect the input area when I get touch events.
I'm almost sure a better way should be using Actor class but there's something I must be doing wrong becase the events never fire.
Here's my code:
...
Texture texture = new Texture(Gdx.files.internal("assets/sprite-sheet.png"));
singlePlayerButton = new Image("SinglePlayerButton", new TextureRegion(texture,0,0,50,50)){
@Override
public boolean touchDown(float x, float y, int pointer) {
// TODO Auto-generated method stub
System.out.println("touch down");
return super.touchDown(x, y, pointer);
}
@Override
public void touchUp(float x, float y, int pointer) {
System.out.println("touch up");
}
};
stage.addActor(singlePlayerButton);
...
public void render(float delta) {
// Clear the screen
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.draw();
spriteBatch.end();
}
The image displays well but it doesn't matter how many times I click on it I never get the event fired. What am i missing? Register the event? I can't find any addTouchListener() method in Stage or Actor class.
Thanks!
Upvotes: 2
Views: 5315
Reputation: 7254
You have to register all input processors with libGDX. Stage implements InputProcessor
, so you have to register it:
@Override
public void create() {
//... initialization
Gdx.input.setInputProcessor(stage);
}
Upvotes: 17