4
\$\begingroup\$

I am creating a game using libgdx.

In the same each level has a class. I have stage as a member variable of the class. To this stage, I add actors.

Inside the levels class, I have attaced the input processor to the stage like below.

stage = new Stage();
sviewPort = new StretchViewport(OSR_Constants.VIEWPORT_WIDTH, OSR_Constants.VIEWPORT_HEIGHT);
OrthographicCamera camera = new OrthographicCamera(OSR_Constants.VIEWPORT_WIDTH, OSR_Constants.VIEWPORT_HEIGHT);
camera.position.set(0, 0, 0);
camera.update();
sviewPort.setCamera(camera);
stage.setViewport(sviewPort);

Gdx.input.setInputProcessor(stage); 

The level object is instantiated in another class known as GameScreen. GameScreen implements the Screen interface.

I am unable to detect touch events on my actors in the stage. Each actor has also been given bounds as per the world coordinates. 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;
      }
  });
\$\endgroup\$
  • \$\begingroup\$ You should disable InputProcessor on Stage when it is not required to kick start your Actor's InputProcessor. Use Gdx.input.setInputProcessor(null); \$\endgroup\$ – kingAm Jan 28 '15 at 4:27
  • \$\begingroup\$ Really late to answer this, but I just want to give a suggestion to make sure to call stage.draw() and stage.act() methods in render() \$\endgroup\$ – WeirdElfB0y Jan 22 '16 at 7:02
0
\$\begingroup\$

You shouldn't create new InputListener, but you need to use one that you register in Gdx.input.setInputProcessor

this.addListener(stage)

Didn't test.

\$\endgroup\$
  • \$\begingroup\$ every actor can have their own inputlistener to handle touch events differently \$\endgroup\$ – rrd Jan 27 '15 at 4:12
  • \$\begingroup\$ but if you create new InputListener, you need to register it on "Gdx.input.setInputProcessor(input_listener);" without it you don't get event to this listener. \$\endgroup\$ – admund Jan 27 '15 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.