This site requires JavaScript, please enable it in your browser!
Greenfoot back
ortizme93
ortizme93 wrote ...

2013/4/19

Zombie spawn

ortizme93 ortizme93

2013/4/19

#
I am making a game where the player is faced against a horde of zombies. I am trying to get it so that the zombies spawn at a quick pace into the world to make the game difficult. My current code is this: time = 50; i = 0;
   public void addZombie()
    {
        
        if(i==time)
        {
           i =0;
            if(time>1)
              time-=5;
              int spawn = Greenfoot.getRandomNumber(3);
            if(spawn == 1 || spawn == 2 || spawn == 0 )
            {
            
              getWorld().addObject(new Zombie(), 550, Greenfoot.getRandomNumber(450) + 100);
            }
              
            
        }
        else
            i++;
  
}
Right now the game starts out with the some zombies already in the world and it will immediately spawn 2 more usually. Then the problem begins and it takes forever for new ones to appear. Any help would be great!
danpost danpost

2013/4/19

#
I believe the problem stems from the statement that subtracts 5 from the time. I do not see any code that counters that statement (prevents 'time' from going below zero, when no more zombies will be spawned). Maybe you should explain what the variable 'time' is supposed to do. Also, the 'if' condition on line 10 will always be 'true'. The previous statement sets 'spawn' to a number between zero and two inclusive.
You need to login to post a reply.