libgdx 动画

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() {
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值