Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx - Actor input events not firing

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!

like image 821
rsan Avatar asked Dec 02 '25 05:12

rsan


1 Answers

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);
}
like image 88
Ludevik Avatar answered Dec 03 '25 21:12

Ludevik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!