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?