以下介绍的是官方的关于Animation的一个例子:跑步动画。
其实在这个例子中,动画的实现原理就是:获取Animation中的每一帧,然后使用SpriteBatch将他画出来
一、初始化方法及常见的API
walkAnimation = new Animation(0.025f, walkFrame);//初始化动画.(每一帧的播放时间,精灵表)..什么事精灵表??也就是说Animation把精灵表中的动画都放一遍 walkAnimation.setPlayMode(walkAnimation.LOOP);//设置动画的播放模式
currentFrame = walkAnimation.getKeyFrame(statetime, false);//获取当前帧.第二个参数表示是否循环播放
二、应用举例
package com.example.groupactiontest; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; public class MyGame implements ApplicationListener { Animation walkAnimation; SpriteBatch batch; TextureRegion[] walkFrame; float statetime ; TextureRegion currentFrame; public int COLS = 6; public int ROWS = 5; @Override public void create() { Texture texture = new Texture(Gdx.files.internal("animation_sheet.png")); TextureRegion[][] temp = TextureRegion.split(texture, texture.getWidth()/COLS, texture.getHeight()/ROWS);//分割animation_sheet。png用于显示 walkFrame = new TextureRegion[COLS*ROWS]; int i,j; int index = 0; for(i = 0 ; i < ROWS ; ++i){//将每一帧的画面装入精灵表 for(j = 0 ; j < COLS ; ++j){ walkFrame[index++] = temp[i][j]; } } walkAnimation = new Animation(0.025f, walkFrame);//初始化动画.(每一帧的播放时间,精灵表)..什么事精灵表??也就是说Animation把精灵表中的动画都放一遍 walkAnimation.setPlayMode(walkAnimation.LOOP);//设置动画的播放模式 batch = new SpriteBatch(); statetime = 0; } @Override public void dispose() { // TODO Auto-generated method stub } @Override public void pause() { // TODO Auto-generated method stub } @Override public void render() { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); statetime += Gdx.graphics.getDeltaTime();//Gdx.graphics.getDeltaTime(): 获取系统的渲染时间 currentFrame = walkAnimation.getKeyFrame(statetime, false);//获取当前帧.第二个参数表示是否循环播放 batch.begin(); batch.draw(currentFrame, 0, 0, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);//将当前帧画出来 batch.end(); } @Override public void resize(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void resume() { // TODO Auto-generated method stub } }
三、效果图
就是一个小人物跑起来了的动画
四、源码下载
http://download.csdn.net/detail/caihongshijie6/7005463