简体   繁体   中英

How to draw LibGDX Sprite from Blank Constructor

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 )?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM