久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Libgdx 中創建一個按鈕?

How to create a button in Libgdx?(如何在 Libgdx 中創建一個按鈕?)
本文介紹了如何在 Libgdx 中創建一個按鈕?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想創建一個在用戶懸停或單擊它時會發生變化的按鈕.我創建了以下變量

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 中的一個角色.要渲染一個演員,您需要使用一個包含屏幕上所有演員的舞臺,渲染它們并更新它們.我假設您想要一個帶有文本的按鈕,因此您應該使用 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.

這篇關于如何在 Libgdx 中創建一個按鈕?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 国产精品明星裸体写真集 | 国产福利视频导航 | 久视频在线 | 毛片在线免费 | 粉嫩高清一区二区三区 | 91精品久久久久久久久久 | 精品欧美一区二区精品久久久 | 国产91综合一区在线观看 | 青青99| 国产不卡一区在线观看 | 精品国产乱码 | 麻豆一区二区三区精品视频 | 精品欧美 | 欧美日韩亚洲一区 | 亚洲欧美中文日韩在线v日本 | 国产精品九九九 | 国产在线精品一区二区三区 | 国产免费一区二区 | 免费国产一区 | 亚洲高清网 | 99久久婷婷国产亚洲终合精品 | 欧美日韩国产精品一区二区 | 国产一区 | 日韩三级| 日批免费看 | 国产成人免费网站 | 第四色播日韩第一页 | 欧美日韩亚洲一区 | 欧美精品一区二区免费 | 九九九久久国产免费 | 日韩欧美国产成人一区二区 | 国产精品毛片无码 | 国产一区二区毛片 | 午夜国产在线 | 日韩精品久久久久久 | 黑人巨大精品欧美一区二区免费 | 亚洲免费视频一区 | 亚洲精品久久久久久一区二区 | 精品国产精品 | 中文字幕1区2区3区 日韩在线视频免费观看 | 91精品国产91久久久久久密臀 |