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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值