如何在Libgdx中创建一个按钮? [英] How to create a button in Libgdx?

查看:185
本文介绍了如何在Libgdx中创建一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个按钮,当用户将鼠标悬停或单击时,该按钮会发生变化。
我创建了以下变量

I want to create a button that changes when the user hovers it, or clicking it. I created the following variable

Button buttonPlay = new Button();

我不知道现在该怎么办,如何加载图片?如何在按钮中写入文字?如何实现事件/效果(悬停,点击)?

I don't know what to do now, how to load the images? How to write text into the button? How to implement the events / effects (hover, click)?

如果有人可以为按钮编写一些示例代码,那将非常有用。

It would be very helpful if someone could write some example code for a button.

推荐答案

按钮只是libgdx中的一个actor。要渲染一个actor,你使用一个包含屏幕所有actor的舞台,渲染它们并更新它们。我假设您需要一个带文本的按钮,因此您应该使用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和
,然后为up,down和checked状态添加字体和纹理。 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.

这篇关于如何在Libgdx中创建一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆