【问题标题】:InputListener doesn't work with OrthographicCamera and ActorsInputListener 不适用于 OrthographicCamera 和 Actors
【发布时间】:2015-01-05 09:19:27
【问题描述】:

我有一个带有 OrthographicCamera 的舞台,还有一个带有 InputListener 设置器的演员,由 addListener (来自演员类)。问题是演员不处理输入,但如果在我的屏幕中删除 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){
        ...
    }
}

【问题讨论】:

  • 您没有在调整大小时更新舞台的视口。
  • 我添加了:stage.getViewport().update(width,height);在 resize 方法中,它不起作用
  • 使用更新(width, height, true)
  • 同时在 render() 中执行stage.act()
  • 那是因为ExtendViewport 让您可以使用“虚拟”分辨率,也可以是 1x1。一个屏幕只能有整个像素,但你仍然可以将你的精灵移动半个像素。如果这不可能,您将遇到很大的问题,因为在 60Hz 帧速率下,您最慢的移动速度将是 60px/s。

标签: java libgdx


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    相关资源
    最近更新 更多