pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

限時(shí)送ChatGPT賬號(hào)..

我想創(chuàng)建一個(gè)在用戶懸?;騿螕羲鼤r(shí)會(huì)發(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)在不知道該怎么辦,如何加載圖像?如何將文本寫入按鈕?如何實(shí)現(xiàn)事件/效果(懸停、點(diǎ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 中的一個(gè)角色.要渲染一個(gè)演員,您需要使用一個(gè)包含屏幕上所有演員的舞臺(tái),渲染它們并更新它們.我假設(shè)您想要一個(gè)帶有文本的按鈕,因此您應(yīng)該使用 TextButton 類并將其添加到舞臺(tái).TextButton 需要一個(gè)要呈現(xiàn)的字符串和一個(gè) ButtonStyle,在本例中是一個(gè) TextButtonStyle,它基本上是一個(gè)包含有關(guān)按鈕的所有信息的類(字體、未按下時(shí)可繪制、按下時(shí)可繪制等).

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)建一個(gè)包含所有紋理的舞臺(tái)、字體和紋理圖集.然后我初始化一個(gè)空的 TextButtonStyle 和我為向上、向下和選中狀態(tài)添加字體和紋理.font、up、down 和 checked 都是 Drawable 類型的靜態(tài)變量,因此您可以真正傳遞任何類型的 Drawable(紋理、9-patch 等).然后只需將按鈕添加到舞臺(tái).

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)在,為了在實(shí)際單擊按鈕時(shí)執(zhí)行某些操作,您必須為按鈕添加一個(gè)偵聽器,即 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");
        }
    });

當(dāng)然,與其將按鈕直接添加到舞臺(tái),不如將其添加到表格并將表格添加到舞臺(tái),但我不想讓這篇文章太混亂.這里 是關(guān)于 libgdx 中表格的一個(gè)很好的教程.

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.

這篇關(guān)于如何在 Libgdx 中創(chuàng)建一個(gè)按鈕?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時(shí)間,就像在 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:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 热工多功能信号校验仪-热电阻热电偶校验仿真仪-金湖虹润仪表 | 专业深孔加工_东莞深孔钻加工_东莞深孔钻_东莞深孔加工_模具深孔钻加工厂-东莞市超耀实业有限公司 | 优秀的临床医学知识库,临床知识库,医疗知识库,满足电子病历四级要求,免费试用 | 钢骨架轻型板_膨石轻型板_钢骨架轻型板价格_恒道新材料 | 溶氧传感器-pH传感器|哈美顿(hamilton) | 圆盘鞋底注塑机_连帮鞋底成型注塑机-温州天钢机械有限公司 | 一氧化氮泄露报警器,二甲苯浓度超标报警器-郑州汇瑞埔电子技术有限公司 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 科客,主见不成见| 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | SMC-ASCO-CKD气缸-FESTO-MAC电磁阀-上海天筹自动化设备官网 | 恒温恒湿试验箱_高低温试验箱_恒温恒湿箱-东莞市高天试验设备有限公司 | 济南货架定做_仓储货架生产厂_重型货架厂_仓库货架批发_济南启力仓储设备有限公司 | 上海盐水喷雾试验机_两厢式冷热冲击试验箱-巨怡环试 | 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 江苏南京多语种翻译-专业翻译公司报价-正规商务翻译机构-南京华彦翻译服务有限公司 | 罗氏牛血清白蛋白,罗氏己糖激酶-上海嵘崴达实业有限公司 | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 华中线缆有限公司-电缆厂|电缆厂家|电线电缆厂家 | 智能化的检漏仪_气密性测试仪_流量测试仪_流阻阻力测试仪_呼吸管快速检漏仪_连接器防水测试仪_车载镜头测试仪_奥图自动化科技 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 板框压滤机-隔膜压滤机配件生产厂家-陕西华星佳洋装备制造有限公司 | 电采暖锅炉_超低温空气源热泵_空气源热水器-鑫鲁禹电锅炉空气能热泵厂家 | 消电检公司,消电检价格,北京消电检报告-北京设施检测公司-亿杰(北京)消防工程有限公司 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | 农业仪器网 - 中国自动化农业仪器信息交流平台 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 航空铝型材,7系铝型材挤压,硬质阳*氧化-余润铝制品 | 深圳诚暄fpc首页-柔性线路板,fpc柔性线路板打样生产厂家 | 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 山东商品混凝土搅拌楼-环保型搅拌站-拌合站-分体仓-搅拌机厂家-天宇 | 智慧食堂_食堂管理系统_食堂订餐_食堂消费系统—客易捷 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 超声波清洗机_大型超声波清洗机_工业超声波清洗设备-洁盟清洗设备 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 骨灰存放架|骨灰盒寄存架|骨灰架厂家|智慧殡葬|公墓陵园管理系统|网上祭奠|告别厅智能化-厦门慈愿科技 | PE拉伸缠绕膜,拉伸缠绕膜厂家,纳米缠绕膜-山东凯祥包装 | 涂层测厚仪_漆膜仪_光学透过率仪_十大创新厂家-果欧电子科技公司 |