0

I know this might already have been answered, but all the places where i found it, it wouldn't work properly. I'm making a game in Greenfoot and I'm having an issue. So I'm generating a random number every time a counter reaches 600, and then testing if that randomly generated number is equal to 1, and if it is, it creates an object. For some reason, the object will be created every time the counter reaches 600. I'm somewhat new to Java so it's probably something simple.

import greenfoot.*;
import java.util.Random;

/**
 * Write a description of class Level_One here.
 * 
 * @CuddlySpartan
 */
public class Level_One extends World
{
Counter counter = new Counter();

/**
 * Constructor for objects of class Level_One.
 * 
 */
public Level_One()
{    
    super(750, 750, 1); 
    prepare();
}

public Counter getCounter()
{
    return counter;
}


private void prepare()
{
    addObject(counter, 150, 40);
    Ninad ninad = new Ninad();
    addObject(ninad, getWidth()/2, getHeight()/2);
    Fail fail = new Fail();
    addObject(fail, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
}

private int spawnCounter = 0;
private int invincibleCounter = 0;
Random random = new Random();
private int randomNumber;


public void act() 
 {
     controls();
 {if (spawnCounter > 500) {
     spawnCounter = 0;
     addObject(new Fail(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
 }
 spawnCounter++;
 {if (spawnCounterTwo > 300) {
     spawnCounterTwo = 0;
     addObject(new APlus(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
   }
 spawnCounterTwo++;
  }

  if (invincibleCounter > 600)
  {
      int randomNumber = random.nextInt(10);

      if (randomNumber == 1)
      {          
          Invincible invincible = new Invincible();
          addObject(invincible, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
          invincibleCounter = 0;
      }
      if (randomNumber == 2)
      {
          Storm storm = new Storm();
          addObject(storm, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
      }
      else 
      {              
      }
    }
 invincibleCounter ++;
}
   }
    private int spawnCounterTwo = 100;

    public void controls()
   {

    if (Greenfoot.isKeyDown("escape"))
    {
            Greenfoot.stop();
    }
   }

}

I'm not getting errors as it is compiling fine, but when i run it i have issues. Any help? Thanks in advance!

2 Answers2

1

This is only speculation, since I cannot see the rest of your code, but I suspect that you are seeding your random number generator with some constant number. So every time you run your program, the random number generator generates numbers in the same order. In order to confirm this, please show some more code.

Also, your brackets do not match, so at least please show enough code to have matching curly braces.

Daniel
  • 6,595
  • 9
  • 38
  • 70
0

Are you sure it is created exactly when the counter hits 600? You're incrementing the counter every frame, and at the default ~30 fps speed, that's twenty seconds. Then every frame after that, you're getting a random integer and have a 10% chance to make an Invincible. But 10% chance will on average come up within ten frames, which is 1/3 of a second. Then the counter will reset and you'll wait twenty more seconds, then create an Invincible within the next second, and so on. If you want a 10% chance every 20 seconds, you need to reset the Counter in the else branch, as well as the "then" branch (or just reset it just inside your very first if).

Neil Brown
  • 3,558
  • 1
  • 27
  • 36