LibGDX遊戲引擎-5-動畫繪製(Animation)

來源:互聯網
上載者:User


LibGDX遊戲引擎-5-動畫繪製 Animation


本系列博文均來自 新浪部落格 - 奮鬥小馬鈴薯丶 的文章,在其基礎上加上了我自己的理解和修改,在此說明!


libgdx給我們提供了一個專門負責管理動畫的Animation類,是專門負責做動畫的管理和放映的


Animation類 (動畫容器)


-------------------------------------------------------------------------------------


Api定義:動畫是由多個幀,在設定的時間間隔序列顯示。比如,一個跑步的人一個動畫可以通過運行時播放這些映像無限拍照他了。


-------------------------------------------------------------------------------------


功能用法:管理動畫,設定隨即播放模式和播放順序。


-------------------------------------------------------------------------------------


使用方法: walkAnimation = new Animation(float fDuration, keyFrames)
  
-------------------------------------------------------------------------------------
    
第一個參數是播放每一幀的時間,後面是一個TestureRegion。下面我給大家介紹下,Animation的原理,顯示了一個完整的運行一個周期。它被稱為精靈表。每個矩形是一個精靈,它被稱為一個架構。先建立運行動畫,在精靈要繪製後,隨著時間的推移,繪製另一個矩形內的圖片。
一般動畫都是用一張圖片,然後配合TextureRegion來實現的。下面我們官方的動畫來做示範


-------------------------------------------------------------------------------------
代碼解讀:


由於代碼比較繁多,而且許多都是馬鈴薯之前講解過的,所以這裡馬鈴薯挑一些比較重要的來講解下。這裡我列舉了幾個,如:TextureRegion[][]數組、setPlayMode()方法、statetime的設定、


(1)TextureRegion[][]數組


代碼:TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() /FRAME_ROWS);


這個段代碼是怎麼回事呢?他是採用分離式的方法分分割傳入的紋理,將獲得的紋理分為一個二維數組。記住,前提是分割的矩形大小相等。然後使用臨時變數,填充walkframes數組。這是樣使用起來很方便。


(2) SetPlayMode()方法


它是Animation類自己封裝的一個方法,是用來設定播放模式的,其中它提供的模式有6種:NORMAL、REVERSED、LOOP、LOOP_REVERSED、LOOP_PINGPONG、LOOP_RANDOM、。
NORMAL:這個不用說了,就是正常的播放模式。
REVERSED:反向播放,從後向前播放,這個就像人物倒退的跑。
LOOP:持續播放,這個比較常用。
LOOP_REVERSED:持續倒退播放。
LOOP_PINGPONG:  向前播放幾張圖片,再向後播放幾幀圖片。


(3)  StateTime 使用 


 代碼stateTime += Gdx.graphics.getDeltaTime(),他是一個擷取一個狀態下所持續的一個時間。就像我們在現實世界使用的時間一樣,一般配合系統時間使用Gdx.graphics.getDeltaTime():擷取系統渲染時間,一般預設是0.173秒。


-------------------------------------------------------------------------------------
詳細實現:


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 walkSheet;
   TextureRegion[] walkFrames;
   SpriteBatch batch;
   TextureRegion currentFrame;


   float stateTime;


   @Override
   public void create() {
       walkSheet = new Texture(Gdx.files.internal("animation_sheet.png"));


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


       walkFrames = 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++) {
               walkFrames[index++] = tmp[i][j];
           }
       }
       walkAnimation = new Animation(0.025f, walkFrames);


       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
   }

}






例子源碼:


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[] walkFrame;
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"));
walkFrame = new TextureRegion[ROW*COL];
array = TextureRegion.split(texture,texture.getWidth()/COL, texture.getHeight()/ROW);//圖片是5行6列
for(int i=0;i<ROW;i++){
for(int j=0;j<COL;j++){
walkFrame[i*COL+j] = array[i][j];
}
}
animation = new Animation(0.05f,walkFrame);
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, 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() {
}


}


轉載請註明出處:blog.csdn.net/qsuron 小樹部落格(qsuron)





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.