libgdx学习记录3——动画Animation

libgdx动画采用Animation实现,即通过帧动画实现。

代码如下:

 1 package com.fxb.newtest;
 2 
 3 import com.badlogic.gdx.ApplicationAdapter;
 4 import com.badlogic.gdx.Gdx;
 5 import com.badlogic.gdx.graphics.GL10;
 6 import com.badlogic.gdx.graphics.Texture;
 7 import com.badlogic.gdx.graphics.g2d.Animation;
 8 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
 9 import com.badlogic.gdx.graphics.g2d.TextureRegion;
10 
11 public class Lib002_Animation extends ApplicationAdapter{
12 
13     Texture texture;
14     Animation animation;
15     SpriteBatch batch;
16     float currentTime = 0;
17     
18     @Override
19     public void create() {
20         // TODO Auto-generated method stub
21         batch = new SpriteBatch();
22         texture = new Texture( Gdx.files.internal( "data/koalio.png" ) );
23         TextureRegion region = new TextureRegion( texture );
24         TextureRegion[] regions = region.split( 18, 26 )[0];
25         
26         animation = new Animation( 0.1f, regions[1], regions[2], regions[3], regions[4] );
27     }
28 
29     @Override
30     public void render() {
31         // TODO Auto-generated method stub
32         Gdx.gl.glClearColor( 1, 1, 1, 1 );
33         Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
34         
35         currentTime += Gdx.graphics.getDeltaTime();
36         TextureRegion region = animation.getKeyFrame( currentTime, true );
37         
38         batch.begin();
39         batch.draw( region, 100, 100, region.getRegionWidth(), region.getRegionHeight() );
40         batch.end();        
41     }
42 
43     @Override
44     public void dispose() {
45         // TODO Auto-generated method stub
46         super.dispose();
47     }
48 
49 }

运行效果:

原文地址:https://www.cnblogs.com/MiniHouse/p/3739615.html