Question

EDIT:
Following Per's answer: I added this and it works fine :

private class GameScreen implements Screen {
         private Stage mStage;
         private InputMultiplexer inputMultiplexer = new InputMultiplexer();

         public GameScreen() {
             Gdx.input.setInputProcessor(inputMultiplexer);
             mStage = new Stage(0, 0, true);
             MyInput mi = new MyInput(){ //which implements inputProcessor
                @Override
                public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                    General.Log("gamescreen touchDown");
                    return false;
                }

                @Override
                public boolean touchDragged(int screenX, int screenY, int pointer) {
                    // TODO Auto-generated method stub
                    return false;
                }
            };
            inputMultiplexer.addProcessor(mi);
            inputMultiplexer.addProcessor(mStage);
         }

I would like to detect a click on a ui actor, I registered stage as the inputProcessor

Gdx.input.setInputProcessor(stage);

And I added this to my actor:

setBounds(0, 0, texture.getWidth(), texture.getHeight());

But still no response...

Was it helpful?

Solution

It is hard to help you without getting more info on your setup. But this code snippet works fine for me, look and see what you do different. And if you cant get it right create a small project with just the code that fails, easier for us to help you then.

    import com.badlogic.gdx.Game;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Screen;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.Texture.TextureFilter;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;
    import com.badlogic.gdx.scenes.scene2d.InputEvent;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.scenes.scene2d.ui.Image;
    import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

    public class MyGame extends Game {

        @Override
        public void create() {
            setScreen(new GameScreen());
        }

        private class GameScreen implements Screen {

            private Stage mStage;

            public GameScreen() {
                mStage = new Stage(0, 0, true);
            }

            @Override
            public void render(float delta) {
                mStage.act(delta);
                Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                mStage.draw();
            }

            @Override
            public void resize(int width, int height) {
                Gdx.gl.glViewport(0, 0, width, height);
                mStage.setViewport(width, height);
            }

            @Override
            public void show() {
                Texture texture = new Texture(Gdx.files.internal("data/libgdx.png"));
                texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
                TextureRegion region = new TextureRegion(texture, 0, 0, 40, 40);

                for (int y = 0; y < 4; ++y) {
                    for (int x = 0; x < 4; ++x) {
                        Image imageActor = new Image(region);
                        imageActor.setPosition(x * 50, y * 50);
                        imageActor.addListener(new ClickListener() {
                           public void clicked(InputEvent event, float x, float y) {
                                System.out.println("clicked");
                            }
                        });
                        mStage.addActor(imageActor);
                    }
                }

                Gdx.input.setInputProcessor(mStage);
            }

            @Override
            public void hide() {}

            @Override
            public void pause() {}

            @Override
            public void resume() {}

            @Override
            public void dispose() {}
        }
    }

With the ApplicationListener:

    import com.badlogic.gdx.ApplicationListener;
    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.Texture.TextureFilter;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;
    import com.badlogic.gdx.scenes.scene2d.InputEvent;
    import com.badlogic.gdx.scenes.scene2d.Stage;
    import com.badlogic.gdx.scenes.scene2d.ui.Image;
    import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;

    public class MyGame implements ApplicationListener {

        private Stage mStage;

        @Override
        public void create() {
            mStage = new Stage(0, 0, true);

            Texture texture = new Texture(Gdx.files.internal("data/libgdx.png"));
            texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
            TextureRegion region = new TextureRegion(texture, 0, 0, 40, 40);

            for (int y = 0; y < 4; ++y) {
                for (int x = 0; x < 4; ++x) {
                    Image imageActor = new Image(region);
                    imageActor.setPosition(x * 50, y * 50);
                    imageActor.addListener(new ClickListener() {
                       public void clicked(InputEvent event, float x, float y) {
                            System.out.println("clicked");
                        }
                    });
                    mStage.addActor(imageActor);
                }
            }

            Gdx.input.setInputProcessor(mStage);
        }

        @Override
        public void resize(int width, int height) {
            Gdx.gl.glViewport(0, 0, width, height);
            mStage.setViewport(width, height);
        }

        @Override
        public void render() {
            mStage.act(Gdx.graphics.getDeltaTime());
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            mStage.draw();        
        }

        @Override
        public void pause() {}

        @Override
        public void resume() {}

        @Override
        public void dispose() {}    
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top