2016-07-13 194 views
0

我之前通過爲每個狀態分別創建一個.png和一個textureAtlas文件來爲精靈(站立/行走/跳躍)設置不同的狀態。它工作正常。現在我將這些單獨的表單合併爲一個PNG,並生成一個.pack文件。現在我可以按地區訪問這些州,但它們不再具有生命力了?只有每個狀態的第一個精靈被繪製,而不是整個動畫。使用單精靈圖片製作動畫精靈

package com.mygdx.game; 

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Input; 
import com.badlogic.gdx.InputProcessor; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.g2d.Animation; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.graphics.g2d.TextureAtlas; 

public class MyGdxGame extends ApplicationAdapter implements InputProcessor { 
    private SpriteBatch batch; 
    private TextureAtlas textureAtlas; 
    private TextureAtlas textureAtlas2; 
    private Animation animation; 
    private Animation animation2; 
    private Animation currentAnimation; 
    private float elapsedTime = 0; 

    @Override 
    public void create() { 
     batch = new SpriteBatch(); 
     textureAtlas = new TextureAtlas(Gdx.files.internal("Wolverine.txt")); 
     animation = new Animation(1/7f, textureAtlas.findRegion("Standing")); 
     animation2 = new Animation(1/7f, textureAtlas.findRegion("Walking")); 
     currentAnimation = animation; 

     Gdx.input.setInputProcessor(this); 
    } 

    @Override 
    public void dispose() { 
     batch.dispose(); 
     textureAtlas.dispose(); 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

     batch.begin(); 
     //sprite.draw(batch); 
     elapsedTime += Gdx.graphics.getDeltaTime(); 
     batch.draw(currentAnimation.getKeyFrame(elapsedTime, true), 0, 0); 
     batch.end(); 
    } 

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

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public boolean keyDown(int keycode) { 
     if (keycode == Input.Keys.LEFT) { 
      currentAnimation = animation2; 
     } 
      return true; 

    } 

    @Override 
    public boolean keyUp(int keycode) { 
     return false; 
    } 

    @Override 
    public boolean keyTyped(char character) { 
     return false; 
    } 

    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
     return false; 
    } 

    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button) { 
     return false; 
    } 

    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer) { 
     return false; 
    } 

    @Override 
    public boolean mouseMoved(int screenX, int screenY) { 
     return false; 
    } 

    @Override 
    public boolean scrolled(int amount) { 
     return false; 
    } 
} 

這是我使用的是與spritesheet

Wolverine.png 
size: 256, 256 
format: RGBA8888 
filter: Linear,Linear 
repeat: none 
Standing 
    rotate: false 
    xy: 0, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 1 
Standing 
    rotate: false 
    xy: 64, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 2 
Standing 
    rotate: false 
    xy: 128, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 3 
Walking 
    rotate: false 
    xy: 192, 0 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 1 
Walking 
    rotate: false 
    xy: 0, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 2 
Walking 
    rotate: false 
    xy: 64, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 3 
Walking 
    rotate: false 
    xy: 128, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 4 
Walking 
    rotate: false 
    xy: 192, 64 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 5 
Walking 
    rotate: false 
    xy: 0, 128 
    size: 64, 64 
    orig: 64, 64 
    offset: 0, 0 
    index: 6 
+0

請附上您的'.pack'文件內容 –

+0

好的,我在原帖 –

回答

3

數據文件中的問題是,你正在向Animation構造函數只有一個動畫幀。準確地說,您正在使用此構造函數:

public Animation(float frameDuration, TextureRegion... keyFrames) 

只有一個幀。您需要傳遞給您想要在動畫中的所有幀。

裏面你走textureAtlas所有站在精靈正在僅舉人大常委會時,實際上他們應該被命名爲例如

Standing0 
    Standing1 
    Standing2 
    ... 

Walking0 
    Walking1 
    Walking2 
    Walking3 
    ... 

他們的順序是動畫。在將這些動畫的文件打包到紋理圖集或熱修復之前,將其重命名爲動畫文件,您可以在.pack文件中重命名它們。

然後你可以使用

public Array<TextureAtlas.AtlasRegion> findRegions(java.lang.String name) 

方法來獲取所有人大常委會/走起路來像幀將它們傳遞給動畫這樣:

textureAtlas.findRegions("Standing") 
+1

中追加了我用於spritesheet的數據文件,儘管我發現即使在sprites被命名爲is時,數據表也能正常工作。我做錯了什麼是我調用'findRegion()'而不是'findRegions()'。 –