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 }
运行效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)