LibGDX快捷按钮单击操作

时间:2016-05-26 18:55:03

标签: java android button libgdx

我是LibGDX的新手。我遇到的一个问题是当按下按钮时按钮会相互反应。例如,在我的下面的代码中,当我按下其中一个按钮时,我希望其他两个按钮自动被按下(三个按钮中只有一个可以在其中一次按下状态)。我遇到的问题是按钮切换状态似乎有相当多的延迟。

public MenuStateTest(GameStateManager gsm) {
    super(gsm);
    cam.setToOrtho(false, MyGame.WIDTH, MyGame.HEIGHT);

    fitViewport = new FitViewport(MyGame.WIDTH, MyGame.HEIGHT);

    stage = new Stage(fitViewport);
    Gdx.input.setInputProcessor(stage);
    font = new BitmapFont();
    skin = new Skin();

    buttonAtlas = new TextureAtlas("buttonTest.pack");
    skin.addRegions(buttonAtlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("1BBlock");
    textButtonStyle.down = skin.getDrawable("T1BBlock");
    button = new TextButton("", textButtonStyle);
    stage.addActor(button);
    button.setPosition(20, 200);
    button.getStyle().checked = button.getStyle().down;
    button.setChecked(false);


        button2Atlas = new TextureAtlas("Button2Test.pack");
        skin.addRegions(button2Atlas);
        textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = font;
        textButtonStyle.up = skin.getDrawable("2BBlock");
        textButtonStyle.down = skin.getDrawable("T2BBlock");
        button2 = new TextButton("", textButtonStyle);
        stage.addActor(button2);
        button2.setPosition(175, 200);
        button2.getStyle().checked = button2.getStyle().down;
        button2.setChecked(true);

        button3Atlas = new TextureAtlas("Button3Test.pack");
        skin.addRegions(button3Atlas);
        textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = font;
        textButtonStyle.up = skin.getDrawable("3BBlock");
        textButtonStyle.down = skin.getDrawable("T3BBlock");
        button3 = new TextButton("", textButtonStyle);
        stage.addActor(button3);
        button3.setPosition(330, 200);
        button3.getStyle().checked = button3.getStyle().down;
        button3.setChecked(true);

}

@Override
protected void handleInput() {
    if(button.isChecked()&& button.isPressed()){
        button.setChecked(false);
        button2.setChecked(true);
        button3.setChecked(true);
    }

    if(button2.isChecked()&& button2.isPressed()){
        button.setChecked(true);
        button2.setChecked(false);
        button3.setChecked(true);
    }

    if(button3.isChecked()&& button3.isPressed()){
        button.setChecked(true);
        button2.setChecked(true);
        button3.setChecked(false);
    }


}

@Override
protected void update(float dt) {
    stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    handleInput();
}

任何帮助都将不胜感激。

感谢。

1 个答案:

答案 0 :(得分:0)

public MenuStateTest(GameStateManager gsm) {
    super(gsm);
    cam.setToOrtho(false, MyGame.WIDTH, MyGame.HEIGHT);

    fitViewport = new FitViewport(MyGame.WIDTH, MyGame.HEIGHT);

    stage = new Stage(fitViewport);
    Gdx.input.setInputProcessor(stage);
    font = new BitmapFont();
    skin = new Skin();

    buttonAtlas = new TextureAtlas("buttonTest.pack");
    skin.addRegions(buttonAtlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("1BBlock");
    textButtonStyle.checked = skin.getDrawable("T1BBlock");
    button = new TextButton("", textButtonStyle);
    stage.addActor(button);
    button.setPosition(20, 200);

    button2Atlas = new TextureAtlas("Button2Test.pack");
    skin.addRegions(button2Atlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("2BBlock");
    textButtonStyle.checked = skin.getDrawable("T2BBlock");
    button2 = new TextButton("", textButtonStyle);
    stage.addActor(button2);
    button2.setPosition(175, 200);

    button3Atlas = new TextureAtlas("Button3Test.pack");
    skin.addRegions(button3Atlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("3BBlock");
    textButtonStyle.checked = skin.getDrawable("T3BBlock");
    button3 = new TextButton("", textButtonStyle);
    stage.addActor(button3);
    button3.setPosition(330, 200);

    ButtonGroup buttons = new ButtonGroup(button1, button2, button3);
    buttons.setMaxCheckCount(1);
}

@Override
protected void handleInput() {

}

@Override
protected void update(float dt) {
    stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
相关问题