我们可以使用Animation来实现。添加一张图片:

Android游戏引擎libgdx使用教程4:舞台和演员的游戏实例

       具体的原理我们看一下Animation类:

Java代码
  1. public class Animation {    

  2. final TextureRegion[] keyFrames;    

  3. public float frameDuration;    

  4. /** Constructor, storing the frame duration and key frames.   

  5.   

  6. * @param frameDuration the time between frames in seconds.   

  7. * @param keyFrames the {@link TextureRegion}s representing the frames. */    

  8. public Animation (float frameDuration, List keyFrames) {    

  9. this.frameDuration = frameDuration;    

  10. this.keyFrames = new TextureRegion[keyFrames.size()];    

  11. for(int i = 0, n = keyFrames.size(); i < n; i++) {    

  12. this.keyFrames[i] = (TextureRegion)keyFrames.get(i);    

  13. }    

  14. }    

  15. /** Constructor, storing the frame duration and key frames.   

  16.   

  17. * @param frameDuration the time between frames in seconds.   

  18. * @param keyFrames the {@link TextureRegion}s representing the frames. */    

  19. public Animation (float frameDuration, TextureRegion... keyFrames) {    

  20. this.frameDuration = frameDuration;    

  21. this.keyFrames = keyFrames;    

  22. }    

  23. /** Returns a {@link TextureRegion} based on the so called state time. This is the amount of seconds an object has spent in the   

  24. * state this Animation instance represents, e.g. running, jumping and so on. The mode specifies whether the animation is   

  25. * looping or not.   

  26. * @param stateTime the time spent in the state represented by this animation.   

  27. * @param looping whether the animation is looping or not.   

  28. * @return the TextureRegion representing the frame of animation for the given state time. */    

  29. public TextureRegion getKeyFrame (float android开发,青软培训