libgdx开发笔记一
屏幕坐标
通过调用Gdx.input.getX()或Gdx.input.getY()获得的坐标是按屏幕坐标返回的,屏幕坐标的原点在屏幕的左上角。参见Learning LibGDX Game Development, 2nd Edition第72页Reading the keyboard/touch/mouse input。
Reading the keyboard/touch/mouse input
Query the system for the last x or y coordinate in the screen coordinates, where the
screen origin is at the top-left corner by calling either Gdx.input.getX() or Gdx.
input.getY(). The different conditions are as follows:
• To find out whether the screen is touched either by a finger or by mouse,
call Gdx.input.isTouched()
• To find out whether the mouse button is pressed, call Gdx.input.
isButtonPressed()
• To find out whether the keyboard key is pressed, call Gdx.input.
isKeyPressed()
2019-04-05
游戏开发和普通的桌面应用开发是类似的,都需要UI和业务逻辑,只不过游戏开发的UI通常需要更加个性化,引擎对UI的支持也没有桌面应用开发的成熟。在LibGDX引擎中,UI是基于Scene2d实现的。Scene2d实现了常用的控件,这些控件也可以作为一个actor脱离Scene2d使用,正如在LibGDX wiki中所说:
Widgets without scene2d.ui
--------------------------
Widgets can be used as simple actors in scene2d, without using tables or the rest of scene2d.ui. Widgets
have a default size and can be positioned absolutely, the same as any actor. If a widget's size is changed, the
`invalidate` Widget method must be called so the widget will relayout at the new size.
Some widgets, such as Table, don't have a default size after construction because their preferred size is
based on the widgets they will contain. After the widgets have been added, the `pack` method can be used
to set the width and height of the widget to its preferred width and height. This method also calls
`invalidate` if the widget's size was changed, and then calls `validate` so that the widget adjusts itself to the
new size.
在MTX中,通常有一个游戏屏幕(实现了游戏逻辑)和若干个UI屏幕(用于实现资源加载、菜单、关卡选择等),游戏屏幕的大小一般比物理屏幕大(规定单独的世界单位,更方便游戏逻辑的计算),UI屏幕的大小通常设置为与物理屏幕的大小相同(使用像素作为单位)。MTX对Scene2d进行了扩展,实现了菜单、按钮、特效等,方便UI界面的实现:
| 类名 | 描述 | 备注 |
|---|---|---|
| EmptyActorLight | 一个空的轻量级actor | 不需要用户交互,适合简单背景对象,也是Text对象的基类 |
| ButtonGame | 游戏按钮 | 基于scene2d.ui中的Button实现 |
| MenuCreator | 菜单创建器 | 提供静态方法方便创建菜单 |
| EffectCreator | 特效创建器 | 提供静态方法方便创建特效,如抖动、渐入、渐出等 |
| SmartActor | 聪明的actor | 为actor增加一些智能的行为,如气球、雪花等,适合复杂动画的背景对像 |
the LibGDX's application life cycle
the architecture of Canyon Bunny
Game,Screen,Stage,Actor 之间的关系
2019-04-06
关于下面几个概念,可以参考http://blog.sina.com.cn/s/blog_940dd50a0101c0l0.html
| 名称 | 描述 | 备注 |
|---|---|---|
| SpriteBatch | SpriteBatch用于绘制二维矩形参考纹理(区域)。SpriteBatch类可用于批量绘图命令和优化处理的GPU。 | SpriteBatch可以把许多相同纹理一起描述并一起送入GPU,同时赋予纹理和坐标以便每个图形的绘制。-------------其实我的理解就是SpriteBatch就是一个画笔,没有画笔是画不了图片的(个人理解)。 |
| TextureAtlas | 通过TexturePacker创建的纹理图 | |
| Texture | 封装了标准的OpenGL ES纹理,图片从原始格式解码并上传到GPU就被称为纹理。 | 其实就是承装获取到的目的图片的容器。-------其实你就直接把Texture当成图片,这样好理解。 |
| TextureRegion | 定义了Texture的一个矩形区域 | 原点位于左上角,y轴向下;实际操作中我们也经常使用图片的一部分,或者将多个图片资源集合在一个图片文件中。而要显示图片的一部分就可以使用TextureRegion类。----------其实就是截图工具,从左上角开始截图,然后可以定义截图的大小(个人理解)。 |
| Sprite | 持有形状、颜色和纹理信息用于绘制2d精灵(使用Batch) | 是TextureRegion的子类,有位置、大小属性;TextureRegion的加强版,比TextureRegion多了一些功能,如:可以指定位置、颜色、旋转等。其实Sprite的功能就是以上的集合。但是Sprite更方便,它用一个对象描述了一切,但是同时加入了很多TextureRegion和Texture没有的东西,如位置、颜色、旋转. |
2019-04-07
Blending is enabled by default. This means that when a texture is drawn, translucent portions of the texture are merged with pixels already on the screen at that location.
When blending is disabled, anything already on the screen at that location is replaced by the texture. This is more efficient, so blending should always be disabled unless it is needed. E.g., when drawing a large background image over the whole screen, a performance boost can be gained by first disabling blending:
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
batch.begin();
batch.disableBlending();
backgroundSprite.draw(batch);
batch.enableBlending();
// Other drawing here.
batch.end();
Note: Be sure to clear the screen each frame. If this is not done, a texture with alpha can be drawn on top of itself hundreds of times, making it appear opaque. Also, some GPU architectures perform better when the screen is cleared each frame, even if opaque images are being drawn over the entire screen._
2019-07-07
https://www.codeandweb.com/texturepacker/tutorials/libgdx-physics
One thing that's very important to realize when working with LibGDX, is that you cannot instantiate most LibGDX classes outside of the create() method. As a Java programmer, you might instinctively want to do something like this:
// BAD EXAMPLE
final SpriteBatch batch = new SpriteBatch();
final Texture img = new Texture("badlogic.jpg");
Go ahead and try that now to see what I happens. You should get an UnsatisfiedLinkError. This is because LibGDX uses a native library, which needs to be loaded into memory before we can start working with it. So the only only safe place we have to instantiate LibGDX objects is in the create method.
2019-07-08
https://rotatingcanvas.com/starting-game-development-in-android-with-libgdx/
resize: In Android phones resize happens when the orientation of phone is changed, e.g. POTRAIT to LANDSCAPE and vice versa so this function is called when the orientation changes.
2019-07-10
Libgdx项目在Android Studio中打包,在as的Terminal终端执行:gradlew dist