public class MyGdxGame implements ApplicationListener {
Animation animation;
Texture texture;
TextureRegion[] walkFrames;
SpriteBatch batch;
TextureRegion textureRegion;
float statetime;
@Override
public void create() {
//获取一张分割成田字型的,由四个小图组成的图片
texture = new Texture(Gdx.files.internal("data/animation_sheet.png"));
//动画就是这四张小图连续动作而成的,把图片分割分别放到TextureRegion数组中
TextureRegion[][] temp = TextureRegion.split(texture,//Texture texture
texture.getWidth()/2, //int tileWidth
texture.getHeight()/2);//int tileHeight
//切完之后把2维数组的textureRegion顺序放到1维中
walkFrames = new TextureRegion[2 * 2];
int index = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
walkFrames[index++] = temp[i][j];
}
}
//设置动画祯的频率
animation = new Animation(0.02f, walkFrames);
batch = new SpriteBatch();
statetime = 0;
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
/*the time span between the current frame and the last frame in seconds.
Might be smoothed over n frames.*/
statetime+=Gdx.graphics.getDeltaTime();
textureRegion = animation.getKeyFrame(statetime,true);//当前时间内的图片,循环
batch.begin();
batch.draw(textureRegion, 300, 300);//动画坐标为(300,300)
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
libgdx 动画
最新推荐文章于 2021-05-30 19:09:48 发布