2016-12-16 10 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オブジェクトには、描画するときのサイズに関する情報は含まれていません。そのためには、幅、高さ、位置の変数など、描画するためのデータを含む独自のクラスを作成できます。

または、既に基本的な位置情報とサイズデータを扱っている組み込みのSpriteクラスを使用できます。デザインの観点から言えば、Spriteは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 
    } 
} 
関連する問題