InputListener 不适用于 OrthographicCamera 和 Actors

InputListener doesn't work with OrthographicCamera and Actors

我有一个带有 OrthographicCamera 的舞台,还有一个带有 addListener 设置器(来自 actor 类)的 InputListener 的 Actor。问题是 actor 不处理输入,但如果在我的屏幕上删除 OrthographicCamera,Actor 会处理输入,因此,使用 OrthographicCamera Actor 不会处理输入,但如果我删除 OrthographicCamera,它会起作用。

有什么建议吗?

我有以下代码

public class Test implements Screen {

    private Game game;
    private Stage stage;
    private MemoryActor actor;
    private AssetManager manager;
    private boolean loaded = false;
    float width, height;
    private OrthographicCamera camera;

    public Test(Game game){
        this.game = game;
        stage = new Stage();
        manager = new AssetManager();
        manager.load("img.png",Texture.class);
        manager.load("img1.png",Texture.class);
        InputMultiplexer im = new InputMultiplexer();
        im.addProcessor(stage);
        Gdx.input.setInputProcessor(im);
        height = Gdx.graphics.getHeight();
        width = Gdx.graphics.getWidth();
        camera = new OrthographicCamera(width, height);
        camera.position.set(((width / 2)), ((height / 2)), 0);
        camera.update();
        stage.setViewport(new ExtendViewport(300,300, camera));
    }

    public void createActor(){
        Texture back = manager.get("img.png", Texture.class);
        actor = new MemoryActor(manager.get("img1.png", Texture.class), back,0,0,50,50);
        actor.setInputListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("down");
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("up");

            }
        });
        stage.addActor(actor);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        if (manager.update()){
            if (!loaded){
                createActor();
                loaded = true;
            }
        }
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }
}

和 MemoryActor:

public class MemoryActor extends Actor {

    ...

    public MemoryActor(){}

    public MemoryActor(Texture texture, Texture texBack, float x, float y, float width, float height){
        ...
    }


    public void setInputListener(InputListener il){
        addListener(il);
    }

    @Override
    public void draw(Batch batch, float alpha){
        ...
    }
}

仅将舞台注册为 InputProcessor 是不够的。还需要在每一帧通过stage.act()触发Stage的事件处理

此外,您需要在调整大小事件发生时正确更新舞台的 Viewport。这可以通过 stage.getViewport().update(width, height, true) 完成。否则,舞台将根据对屏幕尺寸的不正确假设来处理事件,并且还可能呈现您的舞台而不是您想要的方式。 true 很重要,因为它还会将相机置于新屏幕尺寸的中心,这在 UI 的情况下是必需的。