The 1.Animation class describes  API definition: Animations are displayed by multiple frames at a set time interval sequence. For example, a runner with an animation can play these images by running the infinite picture of him. Feature usage: Manage animations, set the playback mode, and play order. How to use: Walkanimation = new Animation (float fduration, keyFrames) The first parameter is the time to play each frame, followed by a testureregion. Below I give you introduce, animation principle, show a complete run a cycle. It is called an elf table. Each rectangle is a sprite, which is called a frame. First create a run animation that, after the sprite is drawn, draws another picture within the rectangle over time. The general animation is to use a picture, and then with Textureregion to achieve. Below our official animation to do demo 2. Code interpretation (1) textureregion[][] Array code: textureregion[][] tmp = Textureregion.split (Walksheet, Walksheet.getwidth ()/Frame_cols, Walksheet.getheight ()/FRAME_ROWS); What's going on with this piece of code? He divides the incoming textures in separate ways, dividing the obtained textures into a two-dimensional array. Remember, the premise is that the split rectangle is of equal size. Then use the TEMP variable to populate the Walkframes array. This is kind of easy to use. (2) Setplaymode () method It is a method of the animation class's own encapsulation, which is used to set the playback mode, wherein it provides 6 kinds of modes: NORMAL, reversed, LOOP, loop_reversed, Loop_ Pingpong, Loop_random,. Normal: This needless to say, is the normal mode of play. Reversed: Reverse play, play forward from the back, this is like a character backwards run. LOOP: Continuous playback, this is more commonly used. Loop_reversed: Continuously rewind playback. Loop_pingpong: Play a few pictures forward, and then play back several frames of pictures. (3) Statetime uses code statetime + = Gdx.graphics.getDeltaTime (), he is a time to get a state that lasts. Just like the time we use in the real world,General mating system Time Use Gdx.graphics.getDeltaTime (): Gets the system render time, the general default is 0.173 seconds. 3. Detailed implementation
1 Packagecom.mygdx.animation;2 3 ImportCom.badlogic.gdx.ApplicationListener;4 ImportCom.badlogic.gdx.Gdx;5 Importcom.badlogic.gdx.graphics.GL20;6 Importcom.badlogic.gdx.graphics.Texture;7 Importcom.badlogic.gdx.graphics.g2d.Animation;8 ImportCom.badlogic.gdx.graphics.g2d.Animation.PlayMode;9 ImportCom.badlogic.gdx.graphics.g2d.SpriteBatch;Ten Importcom.badlogic.gdx.graphics.g2d.TextureRegion; One /** A * Test using animations - * @authorJack (Le Zhi) - * @blog dtblog.cn the * @qq 984137183 - */ - Public classUseanimationImplementsApplicationlistener { - //column + Private Static Final intFrame_cols=6; - //Line + Private Static Final intFrame_rows=5; A at //Animation class - Animation walkanimation; - //Textures - Texture Walksheet; - textureregion[] walkframes; - spritebatch Batch; in textureregion currentframe; - to floatStatetime; + - @Override the Public voidCreate () { * //Textures $Walksheet =NewTexture (Gdx.files.internal ("Animation_sheet.png"));Panax Notoginseng //dividing a texture into 2-dimensional array coordinates -Textureregion[][] Tmp=textureregion.split (Walksheet, Walksheet.getwidth ()/frame_cols, walkSheet.getHeight ()/frame_rows); thewalkframes=NewTextureregion[frame_cols*frame_rows];//an array that stores all the pictures + intIndex=0; A for(inti = 0; i < frame_rows; i++) { the for(intj = 0; J < Frame_cols; J + +) { +WALKFRAMES[INDEX++]=TMP[I][J];//put a picture in an array - } $ } $walkanimation=NewAnimation (0.025f,walkframes);//to play a picture in an array using an animation class -Walkanimation.setplaymode (Playmode.loop_pingpong);//set playback mode, -Batch=NewSpriteBatch (); theStatetime=0f; - }Wuyi the @Override - Public voidResizeintWidthintheight) { Wu - } About $ @Override - Public voidrender () { - Gdx.gl.glClear (gl20.gl_color_buffer_bit); -Statetime+=gdx.graphics.getdeltatime ();//Render Time ACurrentframe=walkanimation.getkeyframe (Statetime,true); + Batch.begin (); theBatch.draw (Currentframe,gdx.graphics.getwidth ()/2,gdx.graphics.getheight ()/2); - batch.end (); $ } the the @Override the Public voidpause () { the //TODO auto-generated Method Stub - in } the the @Override About Public voidResume () { the the } the + @Override - Public voidDispose () { the //TODO auto-generated Method StubBayi the } the -}
The original is written by Bo Master Le Zhi Editor, the copyright belongs to the Bo owner. The original address http://www.dtblog.cn/1143.html reprint please specify the source!
Use of the "open source Java Game Framework Libgdx theme" -09-animation