两个不同的文本按钮在LibGDX上没有响应

时间:2016-04-28 16:31:08

标签: java libgdx

public class GameScreen implements Screen {

    public void show()
    {
        buttonsAtlas = new TextureAtlas("Color.pack"); //button atlas image
        buttonSkin = new Skin();
        buttonSkin.addRegions(buttonsAtlas);
        font = new BitmapFont(Gdx.files.internal("CustomFont.fnt"), false); //the font

        stage = new Stage(); // window is stage
        stage.clear();

        TextButton.TextButtonStyle style = new TextButton.TextButtonStyle(); // button properties
        style.up = buttonSkin.getDrawable("Red");
        style.down = buttonSkin.getDrawable("Blue");
        style.font = font;

        gameButton = new TextButton("Game Screen", style); //button text and style
        gameButton.setPosition(-250, 500); //button location
        gameButton.setHeight(600); //button height
        gameButton.setWidth(1200); //button width

        shopButton = new TextButton("Shop Menuuuu", style); //button text and style
        shopButton.setPosition(100, 500); //button location
        shopButton.setHeight(600); //button height
        shopButton.setWidth(1200); //button width

        stage.addActor(shopButton);
        stage.addActor(gameButton);

        Gdx.input.setInputProcessor(stage);

        gameButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                gameButton.setBounds(-250, 500, 100, 100);
                Gdx.app.log("my app,", "pressed");
                game.setScreen(new inGame(game));
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log("my app", "released");
            }
        });

        shopButton.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                shopButton.setBounds(100, 500, 100, 100);
                Gdx.app.log("my app,", "pressed");
                game.setScreen(new Shop(game));
                return true;
            }

            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                Gdx.app.log("my app", "released");
            }
        });
    }

    public void render(float x)
    {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        game.batch.begin();
        stage.draw();
        game.batch.end();
    }

    public void dispose()
    {
        batch.dispose();
        buttonSkin.dispose();
        buttonsAtlas.dispose();
        stage.dispose();
    }

我想让两个TextButton同时工作。但是,屏幕上始终显示一个按钮,这是添加到舞台的最后一个按钮。在这种情况下,它是gameButton,因为它是添加到舞台的最后一个按钮。

如何让两个按钮都正常工作?

1 个答案:

答案 0 :(得分:0)

设置负坐标没有意义,可能需要了解游戏的坐标系: o,o位于屏幕的左下角 因此,当您给出负值时,该部分不会显示。

所以两个图像在你的情况下是重叠的,舞台给予了参与者的z索引,最后添加的对象将具有最高的z索引,这意味着最后添加的对象将在其他人之上绘制。

您需要做什么:

{{1}}

也在做actionbound里面的actionlistner不是一个好主意