我的用例略有不同,但我遇到了同样的问题。
我有一个对象,它扩展了Actor,我想使用地图集中的一个区域进行绘制。
public class MyObject extends Actor {
private Texture texture;
public MyObject() {
TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
texture = textureAtlas.findRegion("objectTexture").getTexture();
Gdx.app.log(TAG, "Texture width: " + texture.getWidth());
Gdx.app.log(TAG, "Texture height: " + texture.getHeight());
setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
// ...
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture,
worldPosition.x * Constants.STANDARD_TILE_WIDTH, worldPosition.y * Constants.STANDARD_TILE_WIDTH,
texture.getWidth(), texture.getHeight());
}
}
我收到的不是我期望的区域,而是整个图集,所以我记录的纹理宽度和高度为 1024 x 128。
不幸的是,仍然不知道为什么getTexture() 回报太多,但切换到batchDraw(TextureRegion, ...) 至少让我在一个更好的地方。
public class MyObject extends Actor {
private TextureRegion texture;
public MyObject() {
TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
texture = textureAtlas.findRegion("objectTexture");
setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
// ...
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.draw(texture, getX(), getY());
}
}
根据我看到的精灵,提问者看到了一个蓝色方块,原因与我相同;整个图像由getTexture() 加载,因为它从左下角开始,所以您总是看到一个蓝色方块。
使用Sprite(TextureRegion) 构造函数可能也解决了他们的问题。