0
\$\begingroup\$

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);
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

SpriteBatch has to be started before you can use it. First use:

batch.begin()

And at the end use:

batch.end()
\$\endgroup\$
1
  • \$\begingroup\$ ty for the edit \$\endgroup\$
    – Jimmt
    Commented Jan 24, 2013 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .