LibGDX game engine-5-Animation rendering (Animation)

Source: Internet
Author: User
Tags libgdx


LibGDX game engine-5-Animation


This series of blog posts are from Sina's blog-xiaotudou. I have added my own understanding and modifications to this article!


Libgdx provides an Animation class specifically responsible for Animation management and display.


Animation container)


Bytes -------------------------------------------------------------------------------------


Api definition: an animation is displayed by multiple frames at a set interval. For example, a runner can use an animation to play these images during the runtime to take unlimited photos of him.


Bytes -------------------------------------------------------------------------------------


Function usage: Manage the animation and set the playing mode and sequence.


Bytes -------------------------------------------------------------------------------------


Usage: walkAnimation = new Animation (float fDuration, keyFrames)

Bytes -------------------------------------------------------------------------------------

The first parameter is the playback time of each frame, followed by a TestureRegion. Next I will introduce you to the principle of Animation, which shows a complete running cycle. It is called a genie table. Each rectangle is a Genie called a frame. Create an animation first, and draw an image in another rectangle over time after the genie wants to draw it.
Generally, an animation is implemented with an image and TextureRegion. The following is an official animation demonstration.


Bytes -------------------------------------------------------------------------------------
Code explanation:


As there are many codes and many of them have been explained before, it is very important to explain them here. Here I have listed several, such as: TextureRegion [] [] array, setPlayMode () method, statetime settings,


(1) TextureRegion [] [] Array


Code: TextureRegion [] [] tmp = TextureRegion. split (partition sheet, partition sheet. getWidth ()/FRAME_COLS, partition sheet. getHeight ()/FRAME_ROWS );


What is this code? It splits the input texture into a two-dimensional array using a separate method. Remember, the premise is that the split rectangle is of the same size. Then, use the temporary variables to fill in the repeated frames array. This is easy to use.


(2) SetPlayMode () method


It is a method encapsulated by the Animation class and used to set the playback mode. It provides six modes: NORMAL, REVERSED, LOOP, LOOP_REVERSED, LOOP_PINGPONG, and LOOP_RANDOM ,.
NORMAL: this is the NORMAL playback mode.
REVERSED.
LOOP: continuous playback, which is commonly used.
LOOP_REVERSED: continuous playback.
LOOP_PINGPONG: play several pictures forward, and then play several frames backward.


(3) Use StateTime


Code stateTime + = Gdx. graphics. getDeltaTime (), which is a duration for obtaining a state. Like the time we use in the real world, Gdx. graphics. getDeltaTime () is generally used in combination with the system time to obtain the system rendering time. The default value is 0.173 seconds.


Bytes -------------------------------------------------------------------------------------
Detailed implementation:


Package com. potato;


Import com. badlogic. gdx. ApplicationListener;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. graphics. Color;
Import com. badlogic. gdx. graphics. GL10;
Import com. badlogic. gdx. graphics. OrthographicCamera;
Import com. badlogic. gdx. graphics. Texture;
Import com. badlogic. gdx. graphics. Texture. TextureFilter;
Import com. badlogic. gdx. graphics. g2d. Animation;
Import com. badlogic. gdx. graphics. g2d. BitmapFont;
Import com. badlogic. gdx. graphics. g2d. Sprite;
Import com. badlogic. gdx. graphics. g2d. SpriteBatch;
Import com. badlogic. gdx. graphics. g2d. TextureRegion;
Import com. badlogic. gdx. graphics. g2d. freetype. FreeTypeFontGenerator;
Import com. badlogic. gdx. graphics. g2d. freetype. FreeTypeFontGenerator. FreeTypeBitmapFontData;
Import com. badlogic. gdx. graphics. g2d. tiled. TileAtlas;
Import com. badlogic. gdx. graphics. g2d. tiled. TileMapRenderer;
Import com. badlogic. gdx. graphics. g2d. tiled. TiledLoader;
Import com. badlogic. gdx. graphics. g2d. tiled. TiledMap;


Public class Map implements ApplicationListener {
Private static final int FRAME_COLS = 6;
Private static final int FRAME_ROWS = 5;


Animation walkAnimation;
Texture sheet;
TextureRegion [] extends frames;
SpriteBatch batch;
TextureRegion currentFrame;


Float stateTime;


@ Override
Public void create (){
Export sheet = new Texture (Gdx. files. internal ("animation_sheet.png "));


TextureRegion [] [] tmp = TextureRegion. split (partition sheet,
WalkSheet. getWidth ()/FRAME_COLS, walkSheet. getHeight ()
/FRAME_ROWS );


Required frames = new TextureRegion [FRAME_COLS * FRAME_ROWS];
Int index = 0;
For (int I = 0; I <FRAME_ROWS; I ++ ){
For (int j = 0; j <FRAME_COLS; j ++ ){
Repeated frames [index ++] = tmp [I] [j];
}
}
WalkAnimation = new Animation (0.025f, FIG );


WalkAnimation. setPlayMode (walkAnimation. LOOP_PINGPONG );


Batch = new SpriteBatch ();


StateTime = 0f;
}


@ Override
Public void render (){
Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT );


StateTime + = Gdx. graphics. getDeltaTime ();


CurrentFrame = walkAnimation. getKeyFrame (stateTime, true );


Batch. begin ();
Batch. draw (currentFrame, Gdx. graphics. getWidth ()/2,
Gdx. graphics. getHeight ()/2 );
Batch. end ();
}


@ Override
Public void resize (int width, int height ){
// TODO Auto-generated method stub
}


@ Override
Public void pause (){
// TODO Auto-generated method stub
}


@ Override
Public void resume (){
// TODO Auto-generated method stub
}


@ Override
Public void dispose (){
// TODO Auto-generated method stub
}

}






Example source code:


Package com. qsuron;


Import com. badlogic. gdx. ApplicationListener;
Import com. badlogic. gdx. Gdx;
Import com. badlogic. gdx. graphics. Color;
Import com. badlogic. gdx. graphics. GL10;
Import com. badlogic. gdx. graphics. Texture;
Import com. badlogic. gdx. graphics. g2d. Animation;
Import com. badlogic. gdx. graphics. g2d. BitmapFont;
Import com. badlogic. gdx. graphics. g2d. SpriteBatch;
Import com. badlogic. gdx. graphics. g2d. TextureRegion;


Public class MyDemo implements ApplicationListener {
Private static final int ROW = 5;
Private static final int COL = 6;

Private SpriteBatch batch;
Private Animation animation;
Private Texture texture;
Private TextureRegion currentFrame;
Private TextureRegion [] invalid frame;
Private TextureRegion [] [] array;
Private BitmapFont font;
Private float stateTime;

@ Override
Public void create (){
Batch = new SpriteBatch ();
Texture = new Texture (Gdx. files. internal ("data/animation.png "));
Repeated frame = new TextureRegion [ROW * COL];
Array = TextureRegion. split (texture, texture. getWidth ()/COL, texture. getHeight ()/ROW); // The picture contains five rows and six columns.
For (int I = 0; I <ROW; I ++ ){
For (int j = 0; j <COL; j ++ ){
Jsonframe [I * COL + j] = array [I] [j];
}
}
Animation = new Animation (0.05f, animated frame );
Animation. setPlayMode (Animation. LOOP_PINGPONG );
StateTime = 0;
Font = new BitmapFont ();
Font. setColor (Color. RED );
Font. setScale (3.0f );
}

@ Override
Public void dispose (){
Batch. dispose ();
}


@ Override
Public void render (){
Gdx. gl. glClearColor (0, 0, 0 );
Gdx. gl. glClear (GL10.GL _ COLOR_BUFFER_BIT );
StateTime + = Gdx. graphics. getRawDeltaTime ();
CurrentFrame = animation. getKeyFrame (stateTime, true );
Batch. begin ();
Batch. draw (currentFrame, 0,200 );
Batch. draw (texture, 50, 20 );
Font. draw (batch, "X", currentFrame. getRegionX () + 55, currentFrame. getRegionY () + 65 );
Font. draw (batch, "LibGDX-Animation Demo By qsuron", 0,320 );
Batch. end ();
}


@ Override
Public void resize (int width, int height ){
}


@ Override
Public void pause (){
}


@ Override
Public void resume (){
}


}


Reprinted please indicate the source: blog.csdn.net/qsuron Xiaoshu blog (qsuron)





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.