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

2013/11/10

Spawn Problems!

Prish950 Prish950

2013/11/10

#
I want that if you press run 4 zombies spawning in 4 different places so i make a spawner class wich is blank and only for the coordinates and with this class i want to control the zombie spawning. So i write in Spawner class.
public void act() 
    {
        for (int i = 0; i<4; i++)
          {
             ZombieTop zombieTop = new ZombieTop();
             getWorld().addObject(zombieTop, 5, 0);
             
             ZombieRight zombieRight = new ZombieRight();
             getWorld().addObject(zombieRight, 10, 5);
             
             ZombieLeft zombieLeft = new ZombieLeft();
             getWorld().addObject(zombieLeft, 0, 5);
             
             ZombieBot zombieBot = new ZombieBot();
             getWorld().addObject(zombieBot, 5, 10);
             
          }
         
    }    
My Problem is now with that code that it doesn't stop spawning sometimes i get 20 zombies at each place so where is the problem ??
danpost danpost

2013/11/10

#
The main problem is that you created an actor class to do this. The programming of this process could be done in several ways (as is usually the case with programming). I think I would put the main processes in the Zombie class and have only one Zombie class. Unless there are distinct differences in the behavior of the different zombies objects (zombieTop, zombieRight, zombieLeft and zombieBot), you should use the same blueprint (class) to create the four zombies. Location is not a reason to use a separate class for each one. Getting back to how I would accomplish it: I would add a static field to track the number of zombies added to the world or a static boolean that can be used to determine if zombies have been added or not; and a static method to add the four zombies into the world. The world class act method can call the static method which checks the static fields value and determines whether to add zombies into the world or not. My world constructor would set the static field in the Zombie class to its initial state.
Prish950 Prish950

2013/11/10

#
Okay Thank you helped a lot <3
You need to login to post a reply.