2016-12-16 145 views
2

我想縮放紋理以適合我的屏幕,但我不知道如何去做。LibGDX縮放TextureRegion以適應屏幕

private AssetManager assets; 
private TextureAtlas atlas; 
private TextureRegion layer1,layer2,layer3; 

assets = new AssetManager(); 

assets.load("pack.pack", TextureAtlas.class); 

assets.finishLoading(); 

atlas = assets.get("pack.pack"); 

layer1=atlas.findRegion("floor"); 

有沒有辦法縮放紋理?

回答

1

TextureRegion對象不包含任何關於繪製時的大小的信息。爲此,您可以創建自己的包含用於繪製數據的類,例如寬度,高度和位置的變量。

或者你可以使用內置的雪碧類已經處理了很多這樣的基本定位和尺寸數據。從設計角度來看,我認爲雪碧應該避免,因爲它從TextureRegion延伸出來,所以它將資產與遊戲數據混合在一起。最好有一個遊戲對象類。舉例:

public class GameObject { 
    float x, y, width, height, rotation; 
    TextureRegion region; 
    final Color color = new Color(1, 1, 1, 1); 

    public GameObject (TextureRegion region, float scale){ 
     this.region = region; 
     width = region.getWidth() * scale; 
     height = region.getHeight() * scale; 
    } 

    public void setPosition (float x, float y){ 
     this.x = x; 
     this.y = y; 
    } 

    public void setColor (float r, float g, float b, float a){ 
     color.set(r, g, b, a); 
    } 

    //etc getters and setters 

    public void draw (SpriteBatch batch){ 
     batch.setColor(color); 
     batch.draw(region, x, y, 0, 0, width, height, 1f, 1f, rotation); 
    } 

    public void update (float deltaTime) { 
     // for subclasses to perform behavior 
    } 
}