【问题标题】:LibGDX ButtonGroup Control How Many Buttons Can Be DownLibGDX ButtonGroup 控制可以按下多少个按钮
【发布时间】:2016-05-30 15:53:25
【问题描述】:

我对 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;

    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;

    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;

    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());
}

【问题讨论】:

    标签: java android button libgdx


    【解决方案1】:
    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());
    }
    

    【讨论】:

    • 是的,但这里的问题是按钮只有在我放开手指时才会激活,我试图弄清楚如何让它在我触摸按钮时立即激活。
    • 我不想告诉你该怎么做,但我建议想出一种更简单的方法来设置你的游戏控制。当然,这只是个人意见,但我可以告诉你,如果我在玩这样的控制游戏,那会激怒我 :-)。最后一点,如果你坚持这样做,你可以手动覆盖处理输入事件并有一堆 if-then-elses,也许有人甚至会建议它 - 但它不会是我,特别是如果你'想了解图书馆如何运作才能很好地使用它,但这不是正确的做法。祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多