【问题标题】:ImageTextButton LibGDXImageTextButton LibGDX
【发布时间】:2016-07-12 00:34:44
【问题描述】:

我正在和几个朋友一起创建一个游戏,我目前正在处理主菜单。我想出了如何获得常规的旧 TextButtons,但 ImageTextButtons 让我很难过。我有两个麦克风图像,一个打开一个关闭,当用户单击它时它应该会改变。作为本教程的一部分,我使用了一个纹理打包程序:https://www.youtube.com/watch?v=YPxd_xbnIpk

我一直在使用的众多解决方案之一如下: MenuState 类-

Private skin = new Skin;
TextureAtlas buttonAtlas = new TextureAtlas ("mutebuttons.pack");
skin.addRegions(buttonAtlas);
TextButtonStyle buttonStyle = new TextButtonStyle();
ImageTextButtonStyle buttonStyle2 = new ImageTextButtonStyle();
buttonStyle.up = skin.getDrawable("soundIconON");
buttonStyle.down = skin.getDrawable("soundIconOFF"); 

muteButtons.pack 已经包含打开和关闭麦克风的图像(soundIconON/OFF)。

如果您需要更多信息,请告诉我,并提前致谢。 埃德

【问题讨论】:

  • 您发布的代码看起来像是从您的源代码中的多个不同位置剪切和粘贴的,而且您没有说明出了什么问题,因此我们无能为力。无论如何,我认为您想使用 CheckBox 而不是 ImageTextButton。然后您可以将两个麦克风图像放在一个样式中,它会自动在它们之间切换。

标签: java json text libgdx imagebutton


【解决方案1】:

按钮会自动检查它们自己。你不需要做任何特别的事情,总是可以通过.isChecked 获得检查状态。默认情况下,按钮启动时未选中,但您可以在创建时通过setChecked(true) 将按钮更改为选中。

如果您想根据选中状态更改按钮的外观,您只需添加可绘制对象即可。它在内部执行类似If no checked state image just use default image 的操作。

现在对于按钮的类型,我认为您只需要一个图像按钮就可以了,但是任何按钮都可以创建一个外观可变的按钮。 ImageButton 只允许您在按钮的背景上添加图像。这些是 3 个不同按钮的样式:

    TextButton.TextButtonStyle textButtonStyle = 
            new TextButton.TextButtonStyle(
                    Drawable up, //Background for up state
                    Drawable down, //background for down state
                    Drawable checked, //background for checked == true
                    BitmapFont font); //font

    ImageButton.ImageButtonStyle imageButtonStyle = 
            new ImageButton.ImageButtonStyle(
                    Drawable up,
                    Drawable down,
                    Drawable checked,
                    Drawable imageUp, //Image for up state
                    Drawable imageDown, //Image for down state
                    Drawable imageChecked, //Image for checked == true
                    BitmapFont font);

    //Never actually used this one, you made me aware of it's excistance. I think this is a redundant class.
    ImageTextButton.ImageTextButtonStyle imageTextButtonStyle =
            new ImageTextButton.ImageTextButtonStyle(
                    Drawable up,
                    Drawable down,
                    Drawable checked,
                    BitmapFont font);

所以你需要做的就是设置选中的drawable并开始点击按钮。

您也可以在皮肤文件中设置这些,只需从json 调用正确的可绘制名称。这就是我使用文本按钮创建简单切换按钮的方式。

  com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle:
  {
    default: { up: my_up_button, checked: my_checked_button, font: default-font, fontColor: white }
  }

如果我不想在上面添加文本,只需提供一个空字符串即可。或者,您也可以通过单击更改文本。

    final TextButton button = new TextButton("Ewwww! Touch me!", skin);
    button.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            if (button.isChecked())
            {
                button.setText("Now I am unchecked!");
            }
            else
            {
                button.setText("Now I am checked!");
            }
        }
    });

【讨论】:

    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 2016-08-15
    • 2016-04-02
    • 2017-05-08
    相关资源
    最近更新 更多