TextureRegion对象不包含任何关于绘制时的大小的信息。为此,您可以创建自己的包含用于绘制数据的类,例如宽度,高度和位置的变量。
或者你可以使用内置的雪碧类已经处理了很多这样的基本定位和尺寸数据。从设计角度来看,我认为雪碧应该避免,因为它从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
}
}