LibGDX缩放TextureRegion以适应屏幕

时间:2016-12-16 05:25:44

标签: android libgdx

我试图缩放纹理以适合我的屏幕,但我不知道该怎么做。

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 个答案:

答案 0 :(得分: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
    }
}
相关问题