java - 使用 LibGdx 在 Java 中为 Sprite 表设置动画
问题描述
我这里有一些代码,它获取一个精灵表,并通过获取图像的长度和宽度并按行和列将其划分为单独的精灵。这适用于我的测试精灵表,但不适用于我实际将用于我的游戏的那个。
我的动画课在这里:
package Simple;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.Animation;
public class RunningMan {
private Texture texture;
private TextureRegion[] walkFrames;
private Animation walkAnimation;
private float stateTime = 0f;
private TextureRegion currentFrame;
int rows = 5;
int cols = 6;
public RunningMan(Texture texture) {
this.texture = texture;
TextureRegion[][] tmp = TextureRegion.split(texture, texture.getWidth() / cols, texture.getHeight() / rows); // split the sprite sheet up into its 30 different frames
walkFrames = new TextureRegion[rows * cols];
int index = 0;
for(int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
walkFrames[index++] = tmp[i][j]; // Put the 30 frames into a 1D array from the 2d array
}
}
walkAnimation = new Animation(0.06f, walkFrames); // initialize the animation class
stateTime = 0f;
}
public void draw(Batch batch) {
stateTime += Gdx.graphics.getDeltaTime(); // stateTime is used to determine which frame to show
if(stateTime > walkAnimation.getAnimationDuration()) stateTime -= walkAnimation.getAnimationDuration(); // when we reach the end of the animation, reset the stateTime
currentFrame = walkAnimation.getKeyFrame(stateTime); // Get the current Frame
batch.draw(currentFrame,50,50); // draw the frame
}
}
我运行动画的班级在这里:
package Simple;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Animation extends ApplicationAdapter {
SpriteBatch batch;
RunningMan man;
@Override
public void create () {
batch = new SpriteBatch();
man = new RunningMan(new Texture(Gdx.files.internal("WalkingMan.png")));
}
@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
man.draw(batch);
batch.end();
}
}
当我使用 WalkingMan.png 时,代码可以完美运行。但是当我去使用任何其他精灵表时,例如WalkingWoman.jpg(我不担心这个不透明),当相应地调整列和行时,对齐是关闭的。有什么建议么?
解决方案
动画类似乎通过假设 spritesheet 图像中的所有帧均等间隔将源图像拆分为帧。因此,对于 WalkingMan 图像,512x512px 的原始图像被分成 30 帧,每帧 85.33px 宽(512 / 6 列)和 102.4px 高(512 / 5 行)。
如果,如您所说,调整 WalkingWoman 图像的列号和行号(即 1300x935),您将获得 36 帧,每帧宽 144.44 像素(1300 / 9 列),高 233.75 像素(935 / 4 行)。
问题在于,WalkingWoman spritesheet 的每个帧的间距并不均匀。如果你从图像中分割出第一帧,你会得到:
WalkingMan 图像将每个图像放置在精灵表中的相同大小的边界框内。您只需要确保WalkingWoman 工作表的每个框架都以相同的方式均匀分布。
推荐阅读
- android - Recyler 视图没有遍历所有项目,它只显示一个项目
- kernel - I2C 驱动程序更改以识别多条总线
- angular - Ag-grid 在调整屏幕大小时防止水平滚动
- sql - 使用过滤器分组
- python - 基于字典值的 Matplotlib 条形图颜色
- javascript - 地理定位移动测试,无需 https 即可进行网络地理定位
- sql-server - 通过脚本从模板创建数据库
- c# - 后台线程更新值,在非 UI 线程上调用 OnPropertyChanged,因此出现错误
- php - Codeigniter 3中控制器的问题加载视图
- python - Python 3.x 写入电子表格 - 将数据写入 for 循环中的单元格范围,而不仅仅是两个指定的单元格