android - LibGDX 缩放 TextureRegion 以适应屏幕

标签 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");

有没有办法缩放纹理?

最佳答案

TextureRegion 对象不包含任何有关您绘制它时的大小的信息。为此,您可以创建自己的类,其中包含用于绘制它的数据,例如宽度、高度和位置的变量。

或者你可以使用内置的 Sprite 类,它已经处理了很多基本的定位和尺寸数据。从设计的角度来看,我认为应该避免使用 Sprite,因为它从 TextureRegion 扩展而来,因此它将 Assets 与游戏数据混为一谈。最好有一个游戏对象类。一个例子:

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
    }
}

关于android - LibGDX 缩放 TextureRegion 以适应屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41177769/

相关文章:

android - 在等待加入的线程时显示 ProgressDialog

android - 将 SVG 文件与 libgdx 一起使用

android - 我想知道是否有办法将 libgdx 游戏放入 Android 应用程序中?

android - R.id.myEditText 错误//todolist 尝试

java - 推送通知在 Android 中不起作用,代号一

android - 有没有办法加密 iBeacon UUID 传输?

java - 加载 9-patch 图像作为 Libgdx Scene2d 按钮背景看起来很糟糕

android - 为什么我无法从某些设备获取 HPROF 转储?

android - LibGdx 没有在 float 空间中正确绘制圆圈

ios - Libgdx IOS 不工作?