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

如何在 Libgdx 中創(chuàng)建一個按鈕?

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

問題描述

限時送ChatGPT賬號..

我想創(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)!

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

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
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 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 久久久久久久久久久国产 | 99久久久99久久国产片鸭王 | 久久久久中文字幕 | dy天堂| 一区二区精品电影 | 人人擦人人干 | 欧洲精品久久久久毛片完整版 | 国产999精品久久久久久 | 亚洲视频在线免费 | www.久久久久久久久 | 一区二区在线 | 一区二区三区国产 | 91精品国产91久久综合桃花 | 久久国产高清视频 | 亚洲综合视频 | 亚洲区一区二 | 欧美三级免费观看 | 国产欧美综合在线 | 中文字幕亚洲专区 | 亚洲国产精品日韩av不卡在线 | 久久成人精品一区二区三区 | 国内精品视频免费观看 | 日韩一级精品视频在线观看 | 国产精品久久久久久久久久免费 | 精品乱子伦一区二区三区 | 日韩一二三区 | 伊人精品在线视频 | 亚洲精品在线免费看 | 日韩电影免费在线观看中文字幕 | 日韩精品一区二区三区在线 | 99久久精品国产毛片 | 91性高湖久久久久久久久_久久99 | 精品欧美乱码久久久久久 | 在线中文视频 | 亚洲3级 | 亚洲欧美在线观看 | 精品视频一区二区 | av在线播放网站 | 成年人免费看的视频 | 91精品久久久久久久久 | 国产成人精品一区二区 |