我的使用情况略有不同,但我遇到了同样的问题。
我有一个扩展了 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)
构造函数也可能解决他们的问题。