1

编辑:
按照 Per 的回答:我添加了这个并且效果很好:

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);
         }

我想检测对 ui 演员的点击,我注册stage为 inputProcessor

Gdx.input.setInputProcessor(stage);

我将这个添加到我的演员中:

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

但是还是没有反应……

4

1 回答 1

1

如果不获取有关您的设置的更多信息,很难为您提供帮助。但是这个代码片段对我来说很好,看看你做了什么不同。如果您无法正确创建一个仅包含失败代码的小项目,那么我们可以更轻松地为您提供帮助。

    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() {}
        }
    }

使用 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() {}    
    }
于 2014-01-28T07:05:48.757 回答