1
\$\begingroup\$
public class Main extends ApplicationAdapter {

SpriteBatch batch;

OrthographicCamera camera;

Stage stage;

Image greyback;
Image circle;

private float width;
private float height;

@Override
public void create () {

    batch = new SpriteBatch();

    camera = new OrthographicCamera();

    stage = new Stage();
    Gdx.input.setInputProcessor(stage);

    greyback = new Image(new Texture("greyback.png"));
    circle = new Image(new Texture("circle.png"));

    width = Gdx.graphics.getWidth();
    height = Gdx.graphics.getHeight();

    greyback.setWidth(width);
    greyback.setHeight(height);

    circle.setPosition(200,200);

    circle.setBounds(200, 200, circle.getWidth(), circle.getHeight());
    circle.setTouchable(Touchable.enabled);
    circle.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {

            System.out.println("CLICKED");

            return true;

        }

        public void touchUp(InputEvent event, float x, float y, int pointer, int button) {

            System.out.println("REMOVED");

        }

    });

    stage.addActor(greyback);
    stage.addActor(circle);

}

@Override
public void render () {

    stage.draw();

}

} But, when I click on the circle the touchlistener doesnt fire, why?

\$\endgroup\$
2
  • \$\begingroup\$ Have you used a breakpoint to make sure it's not firing or are you just looking for the output in the console? If the output then are you on Desktop or android? System.out.println() only works on desktop. You can use Gdx.app.log() to make sure that it goes to the appropriate output regardless of platform \$\endgroup\$ Apr 1, 2016 at 15:50
  • \$\begingroup\$ im using System.out.println(); on the desktop. \$\endgroup\$ Apr 1, 2016 at 16:07

2 Answers 2

1
\$\begingroup\$

In your call

circle.setBounds(200, 200, circle.getWidth(), circle.getHeight());

The getWidth() and getHeight() calls are methods on Actor, not Image. You have not set the Actor's width and height, so you are setting its bounds to a rectangle with a height and width of 0... no clickable area.

Change the call to

circle.setBounds(200, 200, circle.getImageWidth(), circle.getImageHeight());
\$\endgroup\$
6
  • \$\begingroup\$ circle.getImageWidth() seems to be returning 0, any ideas why? \$\endgroup\$ Apr 1, 2016 at 17:58
  • \$\begingroup\$ According to the source, it looks like the internal width and height do not get set until layout() is called. Not sure why the libgdx devs did this, but perhaps you should call circle.layout() before circle.setBounds() \$\endgroup\$ Apr 1, 2016 at 18:07
  • \$\begingroup\$ the problem still persists... \$\endgroup\$ Apr 1, 2016 at 18:10
  • \$\begingroup\$ I just copied the code from your original post verbatim into a brand new Libgdx project (with my own circle and greyback images) and the event happens just fine. If you aren't using a debugger, I suggest using one and putting a breakpoint in the touchDown listener to make sure that it is not being called. It could be that the println is just being redirected somwhere \$\endgroup\$ Apr 1, 2016 at 19:26
  • \$\begingroup\$ i see, i appreciate it. \$\endgroup\$ Apr 1, 2016 at 20:12
-1
\$\begingroup\$

You need to do this :

stage.act();

This will make your Stage acting, so your Button Listener will work :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .