問題描述
我想創(chuàng)建一個在用戶懸?;騿螕羲鼤r會發(fā)生變化的按鈕.我創(chuàng)建了以下變量
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();
我現(xiàn)在不知道該怎么辦,如何加載圖像?如何將文本寫入按鈕?如何實現(xiàn)事件/效果(懸停、點擊)?
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 中的一個角色.要渲染一個演員,您需要使用一個包含屏幕上所有演員的舞臺,渲染它們并更新它們.我假設您想要一個帶有文本的按鈕,因此您應該使用 TextButton 類并將其添加到舞臺.TextButton 需要一個要呈現(xiàn)的字符串和一個 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();
}
}
那么這里發(fā)生了什么?我正在為buttons.pack"中的按鈕創(chuàng)建一個包含所有紋理的舞臺、字體和紋理圖集.然后我初始化一個空的 TextButtonStyle 和我為向上、向下和選中狀態(tài)添加字體和紋理.font、up、down 和 checked 都是 Drawable 類型的靜態(tài)變量,因此您可以真正傳遞任何類型的 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.
現(xiàn)在,為了在實際單擊按鈕時執(zhí)行某些操作,您必須為按鈕添加一個偵聽器,即 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 中創(chuàng)建一個按鈕?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!