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)的文章 他也说了一般人的回答的几个答案, 回答 ...
随机推荐
- mpvue 无法获取$store的问题
在开发的时候,我们喜欢将一些公共的方法,属性,放在一个特定的位置,例如在mpvue开发小程序的时候, 我们将其放在 vue提供的store里面,或者在mainjs中通过Vue.prototype.xx ...
- gcc版本切换
查看安装的gcc版本 sudo update--alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 100 显示所有版本gcc路径 sudo ...
- elasticsearch索引清理脚本shell
es-index-clear.sh: #!/bin/bash#----------------------------------------------# Module: es-index-clea ...
- Sass:RGB颜色函数-Mix()函数
Mix 函数是将两种颜色根据一定的比例混合在一起,生成另一种颜色.其使用语法如下: mix($color-1,$color-2,$weight); $color-1 和 $color-2 指的是你需要 ...
- Sass--传多个参数
Sass 混合宏除了能传一个参数之外,还可以传多个参数,如: @mixin center($width, $height) { width: $width; height: $height; posi ...
- 返回结果的 HTTP 状态 码
2xx 200:表示从客户端发来的请求在服务器端被正常处理了. 204:该状态码代表服务器接收的请求已成功处理,但在返回的响应报文中 不含实体的主体部分.另外,也不允许返回任何实体的主体. 2 ...
- 【串线篇】Mybatis缓存之一级缓存
1.体会 一级缓存:MyBatis:SqlSesion级别的缓存:默认存在,不需要设置. 机制:只要之前查询过的数据,mybatis就会保存在一个缓存中(Map):下次获取直接从缓存中拿:当前sess ...
- 【串线篇】Mybatis缓存简介
缓存:暂时的存储一些数据:加快系统的查询速度... CPU: 主频:4-2.7GHZ 内存:4G-8G 1333MHZ 2166MHZ CPU:一级缓存(4MB):二级缓存 (16MB); ...
- centos 6.5 配置 DNS
编辑 vi /etc/resolv.conf 修改 DNS nameserver 202.96.134.133 nameserver 202.96.128.86 nameserver 8.8.8.8 ...
- day07作业猜年龄游戏
# 给定年龄,用户可以猜三次年龄 # # 年龄猜对,让用户选择两次奖励 # # 用户选择两次奖励后退出 get_prize_dict = {} # 获取的奖品信息 age = 18 inp_count ...