Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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);
}
share|improve this question
add comment

1 Answer

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

batch.begin()

And at the end use:

batch.end()
share|improve this answer
    
ty for the edit –  Jimmt Jan 24 '13 at 17:28
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.