I am trying to create a game which has sprites and every second another is spawned, i tried using this as a base: https://github.com/libgdx/libgdx/wiki/A-simple-game However when the new one spawns in it destroys the old one and they start to spawn faster and faster. Here is the relevent code:
ownCreate method:
Array<Rectangle> chickens;
chickens = new Array<Rectangle>();
spawnChicken();
New Spawn Chicken method:
private void spawnChicken() {
int choice;
choice = MathUtils.random(0, 3);
switch (choice){
case 0:
chicken.x = MathUtils.random(0,1920-85);
chicken.y = 0;
break;
case 1:
chicken.x = MathUtils.random(0,1920-85);
chicken.y = 1080-85;
break;
case 2:
chicken.x = 0;
chicken.y = MathUtils.random(0,1080-66);
break;
case 3:
chicken.x = 1920-85;
chicken.y = MathUtils.random(0,1080-66);
break;
}
chicken.height = 66;
chicken.width = 85;
chickens.add(chicken);
runningChickens ++;
lastSpawnTime = TimeUtils.nanoTime();
Render method:
if(TimeUtils.nanoTime() - lastSpawnTime > 1000000000)
spawnChicken();
Iterator<Rectangle> iter = chickens.iterator();
while(iter.hasNext()){
Rectangle chicken = iter.next();
//movement
Very similar to the wiki:
private Array<Rectangle> raindrops;
private long lastDropTime; private void spawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = MathUtils.random(0, 800-64);
raindrop.y = 480;
raindrop.width = 64;
raindrop.height = 64;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
raindrops = new Array<Rectangle>();
spawnRaindrop();
if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
Iterator<Rectangle> iter = raindrops.iterator();
while(iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if(raindrop.y + 64 < 0) iter.remove();
}