I'm a bit new to libGDX. My problem is ,in my code, only if I set the texture in the constructor of the Sprite instance, it will draw anything. My question is shouldn't it be working when I only call setTexture() when rendering? Or is there another way to render while changing the texture?
Following code doesn't draw anything.
public Sprite getSprite() {
if (this.sprite == null) {
this.sprite = new Sprite();
}
return this.sprite;
}
public void render(final float deltaTime, final SpriteBatch batch) {
this.getSprite().setTexture(this.getCurrentTexture());
this.getSprite()
.setPosition(this.getPosition().x, this.getPosition().y);
this.getSprite().setRotation(this.getRotation());
this.getSprite().draw(batch);
}
Following is the working code.
public Sprite getSprite() {
if (this.sprite == null) {
this.sprite = new Sprite(this.getCurrentTexture());
}
return this.sprite;
}
public void render(final float deltaTime, final SpriteBatch batch) {
this.getSprite().setTexture(this.getCurrentTexture());
this.getSprite()
.setPosition(this.getPosition().x, this.getPosition().y);
this.getSprite().setRotation(this.getRotation());
this.getSprite().draw(batch);
}