GAME
DESIGN IN LIBGDX
GUIDE – SIMPLE ANIMATION
DESCRIPTION //images that are in a sprite sheet. Essentially, it places the //images of a sprite sheet into an array and
then, based on the time //that has elapsed, displays the
corresponding frame of the animation. //Note: If you wish to use animation inside
of a larger program, it //would be wise to do the animation work
inside of another class to //hide this complexity at the main game
level. public class AnimationDemo1 extends ApplicationAdapter { private TextureAtlas spritesheet; private TextureRegion[] ourImage; private SpriteBatch batch; private final int totalFrames = 25; private float timeSinceStart; @Override public void create () { batch = new SpriteBatch(); spritesheet = new TextureAtlas(Gdx.files.internal("nukeData.txt")); timeSinceStart = 0; ourImage = new TextureRegion[totalFrames]; for (int i=1; i<=totalFrames; i++) //1 to 25 inclusively
{
String imgName = "prefix_" + i;
ourImage[i-1] = spritesheet.findRegion(imgName);
} } @Override public void render () { //CLEAR THE SCREEN Gdx.gl.glClearColor(1,
1, 1, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); //UPDATE TIME timeSinceStart = timeSinceStart + Gdx.graphics.getDeltaTime(); //CALCULATE CURRENT FRAME //We will display 10 frames per second (fps). //By using 'mod 25', we can let the
animation loop on and on. float fps = 12; int currentFrame = (int)(timeSinceStart * fps) % 25; //DRAW THE CURRENT FRAME batch.begin(); batch.draw(ourImage[currentFrame], 160, 160); batch.end(); } |