I currently have a class called Human, composed of several methods such as render() and update(). This is what it looks like:
public class Human implements Entity {
private Texture img;
private Vector2 pos;
private float time;
public Human() {
img = new Texture(Gdx.files.internal("humanFF.png"));
// coordinates of the human initial position
pos = new Vector2(18,420);
}
@Override
public void render(SpriteBatch batch) {
batch.draw(img, pos.x, pos.y);
}
@Override
public void update(float delta) {
}
}
In my gameScreen class, I created an instance of this class as a new object, human = new Human();
To render it, I had to use .update(delta)
and afterward use .render(batch)
thus creating a 'human' on my gameScreen. This is how I did it.
public class GameScreen extends ScreenAdapter implements InputProcessor {
public static final int WIDTH = 1000;
public static final int HEIGHT = 800;
public boolean run = true;
public float offsetX, offsetY = 0;
private final SpriteBatch batch;
private OrthographicCamera camera;
private IsometricRenderer renderer;
private Human human;
public GameScreen(SpriteBatch batch) {
this.batch = batch;
}
@Override
public void show() {
camera = new OrthographicCamera(WIDTH, HEIGHT);
camera.zoom = 0.3f;
camera.position.set(WIDTH / 2f - 500, HEIGHT / 2f, 10);
renderer = new IsometricRenderer();
// instantiate a new human object
human = new Human();
// Tried to create more humans but was not sure how to render them.
List<Human> humanList = new ArrayList<Human>();
for (int i=0; i<3; i++) {
humanList.add(new Human());
}
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
handleInput();
human.update(delta);
camera.update();
batch.begin();
renderer.draw(batch);
human.render(batch);
batch.end();
}
}
My problem now is how do I render multiple humans on my screen without having to re-instantiate a Human()
object? I tried adding new humans using a for loop but have no idea how to render them as seen in my code. I do not want to create repetitive code where I create human1 = new Human()
, human2 = new Human()
etc and render them using the render function one by one.
The approach you described in your question is good. You can create a list of Human
objects and iterate over them to render multiple instances. You just need to use a global list instead of a list that only exists within the show()
method:
public class GameScreen extends ScreenAdapter implements InputProcessor {
public static final int WIDTH = 1000;
public static final int HEIGHT = 800;
public boolean run = true;
public float offsetX, offsetY = 0;
private final SpriteBatch batch;
private OrthographicCamera camera;
private IsometricRenderer renderer;
// private Human human;
private List<Human> humanList = new ArrayList<Human>();
public GameScreen(SpriteBatch batch) {
this.batch = batch;
}
@Override
public void show() {
camera = new OrthographicCamera(WIDTH, HEIGHT);
camera.zoom = 0.3f;
camera.position.set(WIDTH / 2f - 500, HEIGHT / 2f, 10);
renderer = new IsometricRenderer();
// instantiate a new human object
// human = new Human();
// instantiate a list of humans
for (int i=0; i<3; i++) {
humanList.add(new Human());
}
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
handleInput();
for (Human human : humanList) {
human.update(delta);
}
camera.update();
batch.begin();
renderer.draw(batch);
// you can use a for-each loop like the one above or an index-based loop. It doesn't realy matter. Just use the one you prefer
for (int i = 0; i < humanList.size(); i++) {
humanList.get(i).render(batch);
}
batch.end();
}
}
Now you just need to change the position of the humans in the list. Otherwise they will probably all be drawn at the same position, so it seems to be only one human.