2013-02-10 44 views
4

畫LibGDX雪碧我試圖繪製LibGDX一個Sprite。我能做到這一點,如果我用一個構造函數指定一個紋理使用,如如何從空白構造

Sprite sprite = new Sprite(new Texture(Gdx.files.internal("path"))); 

但如果我改用Sprite();和嘗試,然後使用setTexture和/或setRegion,沒有圖像繪製。該API說,一個「紋理,紋理區域,邊界和顏色」,需要進行設置就可以得出任何東西之前。雖然沒有被抽我做了呼叫setTexturesetRegionsetColor

主要問題:如果我使用默認的構造函數Sprite(),我有什麼做的事後,以確保它繪製到屏幕(在SpriteBatch)?

回答

3

我會承擔code'll需要做的非常相同的步驟Sprite(Texture) ctor does

public Sprite (Texture texture) { 
    this(texture, 0, 0, texture.getWidth(), texture.getHeight()); 
} 

public Sprite (Texture texture, int srcX, int srcY, int srcWidth, int srcHeight) { 
    if (texture == null) throw new IllegalArgumentException("texture cannot be null."); 
    this.texture = texture; 
    setRegion(srcX, srcY, srcWidth, srcHeight); 
    setColor(1, 1, 1, 1); 
    setSize(Math.abs(srcWidth), Math.abs(srcHeight)); 
    setOrigin(width/2, height/2); 
} 

這些都是公共方法。