标签:
1 package com.mygdx.animation; 2 3 import com.badlogic.gdx.ApplicationListener; 4 import com.badlogic.gdx.Gdx; 5 import com.badlogic.gdx.graphics.GL20; 6 import com.badlogic.gdx.graphics.Texture; 7 import com.badlogic.gdx.graphics.g2d.Animation; 8 import com.badlogic.gdx.graphics.g2d.Animation.PlayMode; 9 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 10 import com.badlogic.gdx.graphics.g2d.TextureRegion; 11 /** 12 * 测试使用动画 13 * @author Jack(乐智) 14 * @blog dtblog.cn 15 * @qq 984137183 16 */ 17 public class UseAnimation implements ApplicationListener { 18 //列 19 private static final int FRAME_COLS=6; 20 //行 21 private static final int FRAME_ROWS=5; 22 23 //动画类 24 Animation walkAnimation; 25 //纹理 26 Texture walkSheet; 27 TextureRegion[] walkFrames; 28 SpriteBatch batch; 29 TextureRegion currentFrame; 30 31 float stateTime; 32 33 @Override 34 public void create() { 35 //纹理 36 walkSheet = new Texture(Gdx.files.internal("animation_sheet.png")); 37 //将纹理分割成2维数组的坐标 38 TextureRegion[][] tmp=TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS); 39 walkFrames=new TextureRegion[FRAME_COLS*FRAME_ROWS];//存储所有的图片的数组 40 int index=0; 41 for (int i = 0; i < FRAME_ROWS; i++) { 42 for (int j = 0; j < FRAME_COLS; j++) { 43 walkFrames[index++]=tmp[i][j];//将图片放到数组中 44 } 45 } 46 walkAnimation=new Animation(0.025f,walkFrames);//使用动画类播放数组中的图片 47 walkAnimation.setPlayMode(PlayMode.LOOP_PINGPONG);//设置播放模式, 48 batch=new SpriteBatch(); 49 stateTime=0f; 50 } 51 52 @Override 53 public void resize(int width, int height) { 54 55 } 56 57 @Override 58 public void render() { 59 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 60 stateTime+=Gdx.graphics.getDeltaTime();//渲染时间 61 currentFrame=walkAnimation.getKeyFrame(stateTime,true); 62 batch.begin(); 63 batch.draw(currentFrame,Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2); 64 batch.end(); 65 } 66 67 @Override 68 public void pause() { 69 // TODO Auto-generated method stub 70 71 } 72 73 @Override 74 public void resume() { 75 76 } 77 78 @Override 79 public void dispose() { 80 // TODO Auto-generated method stub 81 82 } 83 84 }
标签:
原文地址:http://www.cnblogs.com/AIThink/p/5929063.html