2015-10-01 57 views
0

我有下面的類:定心動畫[libGDX]

public class AnimationDemo implements ApplicationListener { 
    private SpriteBatch batch; 
    private TextureAtlas textureAtlas; 
    private Animation animation; 
    private float elapsedTime = 0; 
    private OrthographicCamera camera; 
    private int width; 
    private int height; 
    private int texturewidth; 
    private int textureheight; 

    @Override 
    public void create() { 
     width = Gdx.graphics.getWidth(); 
     height = Gdx.graphics.getHeight(); 
     camera = new OrthographicCamera(width, height); 
     camera.position.set(width/2, height/2, 0); 
     camera.update(); 


     batch = new SpriteBatch(); 
     textureAtlas = new TextureAtlas(Gdx.files.internal("data/packone.atlas")); 


     textureAtlas.getRegions().sort(new Comparator<TextureAtlas.AtlasRegion>() { 
      @Override 
      public int compare(TextureAtlas.AtlasRegion o1, TextureAtlas.AtlasRegion o2) { 
       return Integer.parseInt(o1.name) > Integer.parseInt(o2.name) ? 1 : -1; 
      } 
     }); 

     animation = new Animation(1/15f, textureAtlas.getRegions()); 

    } 

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

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

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

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

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 
} 

在上述我使用的動畫類簡單地從一個紋理地圖繪製。我正在從另一個SO問題的例子here,但座標不符合我的等式。我應該如何設置這些:

private int texturewidth; 
private int textureheight; 

任何幫助將是巨大的:)

回答

1

你需要關心正確的偏移 - 由來總是在左下角,這就是爲什麼你需要減去一半繪圖時的寬度和高度。

簡而言之它應該是這樣的:

TextureRegion region = animation.getKeyFrame(elapsedTime, true); 
    batch.draw(region, 0 - (region.getRegionWidth()/2f), 0 - (region.getRegionHeight()/2f)); 
+0

感謝您的提示,它的工作原理爲:'batch.draw(區域,camera.position.x - (region.getRegionWidth()/ 2F), camera.position.y - (region.getRegionHeight()/ 2f));' – User3