[英]How to use TextureAtlas for animation instead of TextureRegion? - libgdx
我一直在試圖找出用使用 TextureAtlas 而不是將紋理分成不同區域的東西替換此代碼的最佳方法。 使用它會更容易嗎?
private void loadAllAnimations(){
// Walking animation
Texture texture = Utility.getTextureAsset(characterSpritePath); // Gets default.png
TextureAtlas atlas = Utility.getAtlas(characterAtlasPath); Gets default.atlas
TextureRegion[][] textureFrames = TextureRegion.split(texture, FRAME_WIDTH, FRAME_HEIGHT);
walkDownFrames = new Array<TextureRegion>(3);
walkLeftFrames = new Array<TextureRegion>(3);
walkRightFrames = new Array<TextureRegion>(3);
walkUpFrames = new Array<TextureRegion>(3);
for (int i = 0; i < 4; i++){
for (int j = 0; j < 3; j++){
TextureRegion region = textureFrames[i][j];
if (region == null){
Gdx.app.debug(TAG, "Got null animation frame " + i + "," + j);
}
switch (i){
case 0:
walkDownFrames.insert(j, region);
break;
case 1:
walkLeftFrames.insert(j, region);
break;
case 2:
walkRightFrames.insert(j, region);
break;
case 3:
walkUpFrames.insert(j, region);
break;
}
}
}
walkDownAnimation = new Animation<>(0.25f, walkDownFrames, Animation.PlayMode.LOOP);
walkLeftAnimation = new Animation<>(0.25f, walkDownFrames, Animation.PlayMode.LOOP);
walkRightAnimation = new Animation<>(0.25f, walkRightFrames, Animation.PlayMode.LOOP);
walkUpAnimation = new Animation<>(0.25f, walkUpFrames, Animation.PlayMode.LOOP);
}
這是地圖集和圖像
圖集:
DefaultDood.png
size: 512,64
format: RGBA8888
filter: Nearest,Nearest
repeat: none
DefaltManBack
rotate: false
xy: 342, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManBackWalkin1
rotate: false
xy: 138, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManBackWalkin2
rotate: false
xy: 36, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManForwardWalkin1
rotate: false
xy: 2, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManForwardWalkin2
rotate: false
xy: 206, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManFront
rotate: false
xy: 376, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManLeftStill
rotate: false
xy: 240, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManLeftWalkin1
rotate: false
xy: 70, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManLeftWalkin2
rotate: false
xy: 274, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManRightWalkin1
rotate: false
xy: 308, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManRightWalkin2
rotate: false
xy: 172, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
DefaltManSideRightStill
rotate: false
xy: 104, 2
size: 32, 32
orig: 32, 32
offset: 0, 0
index: -1
精靈表:圖像
如果有什么我搞砸了或很容易解決的問題,我會喜歡的。 (我也在使用 Mastering LibGDX Game Development 一書來指導如何做)。 謝謝!
在原始紋理圖像文件中,在幀號前加上下划線。 例如
然后你可以從地圖集中獲取整個列表作為一個數組傳遞給你的動畫構造函數:
Animation<TextureRegion> defaultManBackWalkinAnimation =
Animation<>(0.25f, textureAtlas.findRegions("DefaltManBackWalkin"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.