InputListener未设置新屏幕

时间:2017-08-08 06:54:11

标签: java android button libgdx

我写了以下代码:

public void show () {
    skin = new Skin (Gdx.files.internal("PlayButton.json"));
    button = new Button(skin);
    button.setPosition(330,600);
    button.setSize(240,240);
    stage.addActor(button);
}

public void render () {
    stage.draw();
    stage.getBatch().begin();
    button.addListener(new InputListener() {
    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                game.setScreen(new GameScreen(game));
            }
        });
        stage.getBatch().end();
}

正如您所看到的,如果单击该按钮,我希望显示GameScreen。我可以点击按钮,但没有反应。 如何设置新屏幕?

1 个答案:

答案 0 :(得分:1)

Button方法中的show()内添加监听器,而不是render()方法。

@Override
public void show () {
    skin = new Skin (Gdx.files.internal("PlayButton.json"));
    button = new Button(skin);
    button.setPosition(330,600);
    button.setSize(240,240);
    button.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            game.setScreen(new GameScreen(game));
            super.clicked(event, x, y);
        }
    });

    stage.addActor(button);
    Gdx.input.setInputProcessor(stage);
}

@Override
public void render (float delta) {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glClearColor(1,1,1,1);

    stage.draw();
    stage.act();
}