4
votes

I'm trying to draw a Sprite in LibGDX. I can do it if I use a constructor that specifies a texture to use, such as

Sprite sprite = new Sprite(new Texture(Gdx.files.internal("path")));

but if I instead use Sprite(); and try to then use setTexture and/or setRegion, no picture is drawn. The API says that a "texture, texture region, bounds, and color" need to be set before anything can be drawn. I've made calls to setTexture, setRegion, and setColor although nothing is being drawn.

Main question: If I use the default Sprite() constructor, what do I have to do afterwards to make sure it draws to the screen (in a SpriteBatch)?

1

1 Answers

3
votes

I'd assume the code'll need to do the very same steps as the Sprite(Texture) ctor does:

public Sprite (Texture texture) {
    this(texture, 0, 0, texture.getWidth(), texture.getHeight());
}

public Sprite (Texture texture, int srcX, int srcY, int srcWidth, int srcHeight) {
    if (texture == null) throw new IllegalArgumentException("texture cannot be null.");
    this.texture = texture;
    setRegion(srcX, srcY, srcWidth, srcHeight);
    setColor(1, 1, 1, 1);
    setSize(Math.abs(srcWidth), Math.abs(srcHeight));
    setOrigin(width / 2, height / 2);
}

These are all public methods.