libgdx-参与者输入事件未启动



我正在测试libgdx,但在处理用户输入时遇到了麻烦。

我的第一次尝试是直接从渲染方法使用Gdx.input,但我想重新发明轮子,因为我正在写很多代码来检测触摸事件时的输入区域。

我几乎可以肯定一个更好的方法应该是使用Actor类,但我一定做错了什么,因为事件永远不会发生。

这是我的代码:

...
    Texture texture = new Texture(Gdx.files.internal("assets/sprite-sheet.png"));
            singlePlayerButton = new Image("SinglePlayerButton", new TextureRegion(texture,0,0,50,50)){
                @Override
                public boolean touchDown(float x, float y, int pointer) {
                    // TODO Auto-generated method stub
                    System.out.println("touch down");
                    return super.touchDown(x, y, pointer);
                }
                @Override
                public void touchUp(float x, float y, int pointer) {
                    System.out.println("touch up");             
                }
            };
            stage.addActor(singlePlayerButton);
...
public void render(float delta) {
        // Clear the screen
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);           
        stage.draw();
        spriteBatch.end();
    }

图像显示得很好,但无论我点击多少次,我都不会触发事件。我错过了什么?注册活动?在Stage或Actor类中找不到任何addTouchListener()方法。

谢谢!

您必须使用libGDX注册所有输入处理器。Stage实现InputProcessor,所以您必须注册它:

@Override
public void create() {
    //... initialization
    Gdx.input.setInputProcessor(stage);
}

最新更新