Libgdx中TextButton的一些思考
版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/caihongshijie6/article/details/37566183
由于有要实现下面TextButton的这个需求。然后就去看了一下Libgdx中文档。
游戏中的button,非常多人都比較习惯使用换图片的方式来实现。
非常少有人会直接使用libgdx中的TextButton。假设实在不行也是自己去写一个TextButton的类。
抱着“它真的有那么渣的态度吗”,我去看了一下libgdx自带的TextButton。下面是我的思考的轨迹。整理例如以下:
在如今,libgdx的资料那么少,有的那些资料也是比較基础的。抱着“看别人的,还不如自己去官方文档。”的态度,自己就開始了下面的历程。。。
1、首先是看了他官方提供的Gdx-test的样例中有下面的这个使用方法:
new TextButton("Flick Scroll", skin.get("toggle", TextButtonStyle.class));
2、这里面用到了Skin这个类,Skin这个类一直被同事诟病。可是我还是抱着学习的态度去看了一下Skin这个类的官方文档。
下面是自己对Skin类学习以后的一些思考与笔记:
http://blog.csdn.net/hjd_love_zzt/article/details/37566435
3、 对TextButton的学习与分析。
对一个类的学习还是依照下面思路:“假设有官方demo。就先去看官方的demo。掌握基本使用以后。然后去看那个类的源代码”。
。
1)下面是自己整理出来的基本使用:
//使用Skin来存储组件的style
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.fontColor = Color.RED;//不起作用
textButtonStyle.font = new BitmapFont(Gdx.files.internal("hjd.fnt"), Gdx.files.internal("hjd.png"),false);
// textButtonStyle.font.setColor(Color.RED);//不起作用
// textButtonStyle.downFontColor = Color.BLUE;
skin.add("style", textButtonStyle);
textButton = new TextButton("hello textButton", skin, "style");
// textButton.getLabel().getStyle().fontColor = Color.YELLOW;//没起作用
// textButton.getLabel().setColor(Color.RED);//没起作用
textButton.setPosition(50, 50);
// textButton.setColor(Color.RED);//不起作用
stage.addActor(image);
stage.addActor(textButton);
2)源代码分析
先贴出TextButton的源代码相关源代码:
这里仅仅看3个函数:。调用TextButton(String,Skin,String)后,它内部会调TextButton(String,TextButtonStyle)这个构造函数。
而这个构造函数中掉了Label的构造函数,所以Style.font、fontColor对象一定要初始化,否则会报对应的异常。
。。
public TextButton (String text, Skin skin, String styleName) {
this(text, skin.get(styleName, TextButtonStyle.class));
setSkin(skin);
}
public TextButton (String text, TextButtonStyle style) {
super(style);
this.style = style;
label = new Label(text, new LabelStyle(style.font, style.fontColor));
label.setAlignment(Align.center);
add(label).expand().fill();
setWidth(getPrefWidth());
setHeight(getPrefHeight());
}
至于draw()函数,我想这就是为什么这个TextButton为什么写的失败的原因了吧。。。。实在是太渣了。。
。。。恩恩。是的。
事实证明官方的TextButton确实是太渣了。
。
。想要设置个字体的颜色都做不到。。
。
。使用提供的API没有效果。点进去看,有的函数竟然还没有实现。
。。
草。。
。
。。
public void draw (SpriteBatch batch, float parentAlpha) {
Color fontColor;
if (isDisabled && style.disabledFontColor != null)
fontColor = style.disabledFontColor;
else if (isPressed() && style.downFontColor != null)
fontColor = style.downFontColor;
else if (isChecked && style.checkedFontColor != null)
fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;
else if (isOver() && style.overFontColor != null)
fontColor = style.overFontColor;
else
fontColor = style.fontColor;
if (fontColor != null) label.getStyle().fontColor = fontColor;
super.draw(batch, parentAlpha);
}
Libgdx中TextButton的一些思考的更多相关文章
- Libgdx New 3D API 教程之 -- Libgdx中使用Materials
This blog is a chinese version of xoppa's Libgdx new 3D api tutorial. For English version, please re ...
- 关于《Head First Python》一书中print_lol()函数的思考
关于<Head First Python>一书中print_lol()函数的思考 在<Head First Python>第一章中,讲述到Python处理复杂数据(以电影数据列 ...
- 简述C#中IO的应用 RabbitMQ安装笔记 一次线上问题引发的对于C#中相等判断的思考 ef和mysql使用(一) ASP.NET/MVC/Core的HTTP请求流程
简述C#中IO的应用 在.NET Framework 中. System.IO 命名空间主要包含基于文件(和基于内存)的输入输出(I/O)服务的相关基础类库.和其他命名空间一样. System.I ...
- c中#与##的应用思考
c中#与##的应用思考 原创 2014年02月25日 22:01:35 927 一. 思考出处 在读<<linux 0.12完全剖析>>初始化部分, init进程是通过fork ...
- 关于HashMap中hash()函数的思考
关于HashMap中hash()函数的思考 JDK7中hash函数的实现 static int hash(int h) { h ^= (h >>> 20) ^ (h >&g ...
- 关于libgdx中UI控件的旋转和缩放的备忘
最近遇到这样一个问题,定义了一个ImageButton后,想对按钮进行下旋转,结果setRotation(-90f),不起作用.后来在官网上找到了原因 关于UI控件的旋转 缩放官网上有这样一段话(链接 ...
- 关于在框架中使用curl的思考,以及,curl其实很好用
初步猜想: 在接触到框架文档的第一阶段时,会觉得控制器调用模型就是一件很简单的事,tp中用D方法或者M方法来实例化模型,laravel中用命名空间来加载模型,CI中用$this->load-&g ...
- Android研发中对String的思考(源代码分析)
1.经常使用创建方式思考: String text = "this is a test text "; 上面这一句话实际上是运行了三件事 1.声明变量 String text; ...
- php中的session过期思考一二
看了php开发组成员鸟哥的一篇关于php设置session过期(http://www.laruence.com/2012/01/10/2469.html)的文章 他也说了一般人的回答的几个答案, 回答 ...
随机推荐
- Spring STS 开发IDE下载安装
https://spring.io/tools https://spring.io/tools3/sts/all/ Spring STS,全称是SpringSource Tool Suite ,是由s ...
- 一、RequireHttps
一.RequireHttps 强制使用Https重新发送请求:如: public class HomeController : Controller { [RequireHttps] public A ...
- 用Matlab的.m脚本文件处理实验室数据
找到相应的文件 findfile %1 打开文件夹 %2 拷贝第一个文件 %3 关闭当前文件,再次拷贝新的文件,直到文件末尾结束 clc clear DST_PATH_t = 'C:\Users\Ma ...
- 快速失败(fail—fast)和 安全失败(fail—safe)
快速失败(fail-fast) 在用迭代器遍历一个集合对象时,如果遍历过程中对集合对象的结构进行了修改(增加.删除),则会抛出Concurrent Modification Exception. 原理 ...
- python基础:4.请至少列举5个 PEP8 规范(越多越好)。
1.变量命名规则: 不能与关键字重名,必须以数字字母下划线组成,且不能以数字开头 2.导包规则: # 推荐这样写 import random import sys # 不推荐这样写 import ra ...
- 分组函数 partition by 的详解,与order by 区别
partition by关键字是分析性函数的一部分,它和聚合函数(如group by)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录, partition by ...
- Python中的"Special Method"
The first thing to know about special methods is that they are meant to be called by the Python inte ...
- less和vim中使用正则表达式搜索
使用less查看 txt 文件之后,按\可以正则表达式来搜索: less phonelist.txt (232) 298-2265 (624) 381-1078 (540) 126-1980 (874 ...
- JavaScript之BOM+DOM
BOM 浏览器对象模型, 用于把浏览器相关的组件封装为对象进行操作. BOM是包含了DOM的. window对象 弹出框相关 确认: 取消: 与打开关闭window有关的方法 定时器相关 暂停选老婆 ...
- Xcode7.1环境下上架iOS App到AppStore 流程②
前言部分 part二部分主要讲解 iOS App IDs 的创建.概要文件的配置.以及概要文件安装的过程. 一.iOS App IDs 的创建 1)进入如图1所示界面点击右上角箭头所指的加号 进入iO ...