0
\$\begingroup\$

I´ve built a gameloop where I generate new items depending on the elapsed time and want to place them somewhere on the screen. I use some randomness for the color and coordinates. For better design I want the sprites of these items to be generated by classes that contain additional information on these items. But I figured out that when doing so my sprites won´t be drawn. Sprites that are generated explictly inside my loop are perfectly drawn.

This is my current gameloop

public void loopGame(float delta) {

    Gdx.gl.glClearColor(255, 255, 255, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    timer.addDelta(delta);

    Gdx.app.debug("Bubbles", "Time: " + String.valueOf(this.timer.getCurrentTime()));

    batch.begin();

    Sprite freeBubble = new Sprite(new Texture(Gdx.files.internal("data/img/bubble_black.png")));
    freeBubble.setPosition(200, 200);
    freeBubble.draw(batch);

    Bubble newBubble = this.bubbleFlask.createNewBubble(timer.getCurrentTime());
    if (newBubble != null) {
        bubbles.add(newBubble);
        Gdx.app.debug("Bubbles", "Created new bubble!");
        Gdx.app.debug("Bubbles", "Attributes: " + newBubble);
    }

    // draw all bubbles
    for (Bubble bubble : bubbles) {
        Gdx.app.debug("Bubbles", "Drawing bubble");
        Sprite bubbleSprite = bubble.buildSprite();
        bubbleSprite.draw(batch);
    }

    batch.end();

}

The freebubble is perfectly drawn. this.bubbleFlask generates bubbles the way it is supposed to but those bubbles are not drawn inside the for loop. Debugging messages show me that the loop works as it should.

So you might guess that bubble.buildSprite() is broken. Here is this method:

public Sprite buildSprite() {
    Sprite sprite = new Sprite();
    sprite.setTexture(new Texture(Gdx.files.internal("data/img/bubble_black.png")));
    sprite.setPosition(300f, 300f);
    return sprite;
}

For the sake of demonstration I placed the same texture file here.

What am I missing here?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Seems like buildSprite is broken indeed. The freeBubble sprite is created with a different constructor, which seems to be the only difference between a properly drawn sprite and those made by buildSprite. Sprite's javadocs state that its parameterless constructor gives you instances that need more than just textures to be set. On the contrary, calling new Sprite(texture) gives you a ready sprite. From Sprite.java:

/** Creates an uninitialized sprite. The sprite will need
/** a texture region and bounds set before it can be drawn. */
public Sprite () {
    setColor(1, 1, 1, 1);
}

/** Creates a sprite with width, height, and texture region 
/** equal to the size of the texture. */
public Sprite (Texture texture) {
    this(texture, 0, 0, texture.getWidth(), texture.getHeight());
}
\$\endgroup\$

You must log in to answer this question.

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