Rahul Dhamecha
Rahul Dhamecha

Reputation: 23

Unable to detect touchdown events on actors in stage LIBGDX

I am making a game in libgdx.

Each Level has a class. I have added a stage to each level. Actors have also been added to this stage. I have used to detect events on the stage.

Gdx.input.setInputProcessor(stage); 

The level object is instantiated in another class known as GameScreen. GameScreen extends the Game class.

I am unable to detect touch events on my actors in the stage. I have added the following code on each actor to detect touch.

this.addListener(new InputListener(){
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
          System.out.println("touchdown at " + x + " " + y);
          return true;
      }
  });

Is there something I am missing. Is my Levels class supposed to extend another class?

Upvotes: 2

Views: 738

Answers (1)

Mobador
Mobador

Reputation: 101

Do you set bounds while creating new actor? It's important, every actor should have bounds set, or it will not register any touches. So, actor constructor should look something like:

    public someActor(){
    setBounds(someActorXPosition, someActorYPosition, someActorWidth, someActorHeight);
    this.addListener(new InputListener(){
...rest of your code...

And then, anytime you move your actor, dont forget to set new bounds. Worked for me

Upvotes: 1

Related Questions