You could use an entity system for this:
Create an abstract entity class:
public abstract class Entity {
public abstract create();
public abstract void render(SpriteBatch sb);
public abstract void update(float dt);
public abstract dispose();
}
Extend the class in you game object:
public class SomeObject extends Entity {
private float x;
private float y;
private float width;
private float height;
private texture someTexture;
@Override
public create() {
...
}
@Override
public void render(SpriteBatch sb) {
// Should also do some checking before rendering, if the entity is visible.
sb.draw(someTexture, x, y, width, height);
}
@Override
public void update(float dt) {
...
}
@Override
public dispose() {
...
}
}
Create an array of your entities:
private ArrayList<Entity> entities = new ArrayList<>();
// Will be used in update(float dt)
private List<Entity> toBeRemoved = new LinkedList<>();
// Add your entities
entities.add(new SomeObject(...));
Rendering:
public void render(SpriteBatch sb) {
sb.begin();
for (Entity e : entities) {
e.render(sb);
}
sb.end();
}
Updating:
public void update(float dt) {
toBeRemoved.clear();
for (Entity e : entities) {
// Should the entity be removed
if (e.removePending()) {
toBeRemoved.add(e);
}
}
entities.removeAll(toBeRemoved);
}