且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Libgdx 中创建一个按钮?

更新时间:2023-11-17 16:04:40

按钮只是 libgdx 中的一个角色.要渲染一个演员,您需要使用一个包含屏幕上所有演员的舞台,渲染它们并更新它们.我假设您想要一个带有文本的按钮,因此您应该使用 TextButton 类并将其添加到舞台.TextButton 需要一个要呈现的字符串和一个 ButtonStyle,在本例中是一个 TextButtonStyle,它基本上是一个包含有关按钮的所有信息的类(字体、未按下时可绘制、按下时可绘制等).

A button is simply an actor in libgdx. To render an actor you use a stage that contains all the actors of the screen, renders them and updates them. I assume you want a button with text, so you should use the class TextButton and add it to a stage. A TextButton takes a string to render and a ButtonStyle, in this case a TextButtonStyle, which is basically a class that contains all the information about the button (font, drawable to render while not pressed, drawable to render while pressed etc).

   public class ButtonExample extends Game{

    Stage stage;
    TextButton button;
    TextButtonStyle textButtonStyle;
    BitmapFont font;
    Skin skin;
    TextureAtlas buttonAtlas;

    @Override
    public void create() {      
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);
        font = new BitmapFont();
        skin = new Skin();
        buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
        skin.addRegions(buttonAtlas);
        textButtonStyle = new TextButtonStyle();
        textButtonStyle.font = font;
        textButtonStyle.up = skin.getDrawable("up-button");
        textButtonStyle.down = skin.getDrawable("down-button");
        textButtonStyle.checked = skin.getDrawable("checked-button");
        button = new TextButton("Button1", textButtonStyle);
        stage.addActor(button);
    }

    @Override
    public void render() {      
        super.render();
        stage.draw();
    }
}

那么这里发生了什么?我正在为buttons.pack"中的按钮创建一个包含所有纹理的舞台、字体和纹理图集.然后我初始化一个空的 TextButtonStyle 和我为向上、向下和选中状态添加字体和纹理.font、up、down 和 checked 都是 Drawable 类型的静态变量,因此您可以真正传递任何类型的 Drawable(纹理、9-patch 等).然后只需将按钮添加到舞台.

So whats happening here? I am creating a stage, a font and a textureatlas with all the textures for the buttons in "buttons.pack". Then I initialize an empty TextButtonStyle and and i add the font and the textures for the up, down and checked states. font, up, down and checked are all static variables of type Drawable so you can really pass it any kind of Drawable (texture, 9-patch etc). Then simply add the button to the Stage.

现在,为了在实际单击按钮时执行某些操作,您必须为按钮添加一个侦听器,即 ChangeListener.

Now in order to do something when the button is actually clicked, you have to add a listener to the button, a ChangeListener.

    button.addListener(new ChangeListener() {
        @Override
        public void changed (ChangeEvent event, Actor actor) {
            System.out.println("Button Pressed");
        }
    });

当然,与其将按钮直接添加到舞台,不如将其添加到表格并将表格添加到舞台,但我不想让这篇文章太混乱.这里 是关于 libgdx 中表格的一个很好的教程.

Of course instead of adding the button directly to the Stage you should add it to a Table and add the Table to the Stage but I didn't want to make this post too confusing. Here is a good tutorial on tables in libgdx.