3

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();
   }
user3165683
  • 347
  • 1
  • 9
  • 28
  • Not enough code is shown to understand this. On your fourth line, you add a `chicken`. What `chicken` is this? A member variable? What does `spawnChicken()` do, assign a new Rectangle instance to a member variable called `chicken`? And where in your code are your first four lines, in the `create()` method while the rest is in `render()`? – Tenfour04 Jul 15 '14 at 16:43
  • Can you show the complete `spawnChicken()` method? – Tenfour04 Jul 15 '14 at 16:57
  • I don't know if code is still missing, but I would expect `spawnChicken()` to start with the line `Rectangle chicken = new Rectangle();`. If `chicken` is a member variable, as it appears it must be if you didn't leave out any code, then your `spawnChicken()` method is simply moving the same chicken around each time you call it. – Tenfour04 Jul 15 '14 at 17:29

1 Answers1

0

Put

Rectangle chicken = new Rectangle();

at the start of spawnChicken() method.


Reasons

  • If you keep chicken a member, it will be replaced every time spawnChicken() method is called.
  • Also, the same object is added to the array every time you try to spawn a chicken.
Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45