前言
放假之后又要清洁, 现在又要拜年, 学习libgdx 的进度慢了很多
声明
libgdx :https://code.google.com/p/libgdx/
夜明的孤行灯 - android游戏开发框架libgdx的使用(四) --舞台和演员: http://www.cnblogs.com/htynkn/archive/2011/11/15/libgdx_4.html
正文
libgdx 的开发教程中, 其中夜明的孤行灯算是比较早, 但正因为时间较前, 所以会出现部分无法编译的代码.
因为夜明的孤行灯用的0.9.2 版本和现在的0.9.8 版本有部分出入, 这里就以0.9.8 版本为准.
首先是舞台的声明和使用.这里和0.9.2 版本没有什么区别. 依然按照夜明的孤行灯的编写.
然后是Actor 类.这里libgdx 的作者将成员变量(Menber variables)从共有(public)改成私有(private), 并添加了setter/getter 方法. 这是一个很好的现象.但本人表示奇怪, 一开始为甚不干脆这样做呢?
0.9.2 版本
this.height = texture.getHeight(); this.width = texture.getWidth(); //draw(SpriteBatch, float) 方法 batch.draw(texture, this.x, this.y);
0.9.8 版本
this.setHeight(texture.getHeight); this.setWidth(texture.getWidth); //draw(SpriteBatch, float) 方法 batch.draw(texture, this.getX(), this.getY());
其他的就不一一举例了.
然后是 Lintener.
在0.9.2 版本中, 是在Actor 类下就有一个 touchUp/Dragged/Down(float, float, int) 方法.
当时我看到这种编写方式, 第一个想法就是, 浪费资源.
现在0.9.8 版本已经放弃了这种方式, 使用Java 的那种委托方式. 我以NarrowButton 类为例.
public NarrowButton() { this.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { return super.touchDown(event, x, y, pointer, button); } }); }
然后又回到Actor 类这边,
之前在0.9.2 版本中,存在构造方法(Constructor)Actor(String) .
但是在0.9.8 版本中, 这个 构造 方法 (Constructor) 不存在了. 又是我的疑问, 这个构造方法 (Constructor) 本来就没什么问题, 为甚删去呢? 现在要这样声明,button = new NarrowButton("narrow");
button = new NarrowButton(); button.setName("narrow");
感觉更复杂了....
好了, 现在有了setter, 估计还有有一个getter. 在libgdx, 从Stage 上找Actor 的方法是findActor(String)~
又是0.9.2 版本
Actor actor = this.parent.findActor("renwu");
0.9.8 版本是这样的
Actor actor = getParent().findActor("renwu");
因为我的InputListener 是NarrowButton 下的内部类, 如果加上this. 就变了是InputListener.getParent(), 而不是NarrowButton.getParent().
结语
版本的更新, 带来旧版本的不适应不兼容.
0.9.8 版本相比0.9.2 版本, 有些地方更新得不错, 有些地方却变得复杂了..
是好是坏, 效率说了算~(感觉coder 的地位真低...)