Reputation:
I'm trying to scale the Texture to fit to my screen, but I'm not sure how to do it.
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");
Is there a way to scale the texture?
Upvotes: 2
Views: 2663
Reputation: 93902
A TextureRegion object does not contain any information about what size it will be when you draw it. For that, you can create your own class that contains data for drawing it, such as variables for width, height, and position.
Or you can use the built-in Sprite class which already handles a lot of basic positioning and size data like that. From a design perspective, I think Sprite should be avoided because it extends from TextureRegion, so it conflates assets with game data. It would be preferable to have a game object class. An example:
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
}
}
Upvotes: 1