CodenameLambda1
CodenameLambda1

Reputation: 1299

What am I missing in libGDX to get InputEvent on Actor?

I am using libGDX on Android. I set up the Stage and added a CustomActor with a custom draw. It's working properly. However, I am not able to get any logs for the touchDragged method via the InputListener for this actor. Even the code within it does not run.

Here is the required exposure to the code:

public class CustomActor extends Actor {
    public CustomActor() {
        this.setListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }
            @Override
            public void touchDragged(InputEvent event, float x, float y, int pointer) {
                //This log doesn't print up!
                Gdx.app.log("CustomActor","touchDragged");
                //This code doesn't work either
                Vector2 v = CustomActor.this.localToParentCoordinates(new Vector2(x,y));
                CustomActor.this.setPosition(v.x, v.y);  
            }
        });
    }
}

Can anyone help me out here on what am I missing ?

Upvotes: 0

Views: 269

Answers (2)

ilkinulas
ilkinulas

Reputation: 3675

  1. try using
    public boolean addListener (EventListener listener) {...} 
    method of Actor. Which version of libgdx are you using? I think actor does not have "setListener" method.
  2. Check your actor bounds. (width & height)

Upvotes: 0

bemeyer
bemeyer

Reputation: 6231

Add the Stage you are using as InputProcessor to receive the events else the Stage can't forward the event to the actors.

Gdx.input.setInputProcessor(stage);

Should do it.

Upvotes: 1

Related Questions