What do you mean with 'it doesn't work'?
What I think would happen is that you create so many actors that the scenario visually stops because it is very slow due to all the actors.


1 | addObject( new NameOfTheClass(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight())); |
1 | addObject( new NameOfTheClass(),Greenfoot.getRandomNumber( 2 )*getWidth(),Greenfoot.getRandomNumber( 2 )*getHeight()); |
1 | addObject( new NameOfTheClass(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight())); |
1 | addObject( new NameOfTheClass(),Greenfoot.getRandomNumber( 2 )*getWidth(),Greenfoot.getRandomNumber( 2 )*getHeight()); |
1 2 3 4 5 6 7 8 | int [] x = {x1,x2,x3,...} int [] y = {y1,y2,y3,...} double chance = 0.01 // for a 1/100 public void act() { if (Math.random() < chance) { addObject( new NameOfTheClass(),x[Greenfoot.getRandomNumber(x.length)],y[Greenfoot.getRandomNumber(y.length)]); } } |
1 | addObject( new NameOfTheClass(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight())); |
1 | addObject( new NameOfTheClass(),Greenfoot.getRandomNumber( 2 )*getWidth(),Greenfoot.getRandomNumber( 2 )*getHeight()); |
1 2 3 4 5 6 7 8 | int [] x = {x1,x2,x3,...} int [] y = {y1,y2,y3,...} double chance = 0.01 // for a 1/100 public void act() { if (Math.random() < chance) { addObject( new NameOfTheClass(),x[Greenfoot.getRandomNumber(x.length)],y[Greenfoot.getRandomNumber(y.length)]); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | import greenfoot.*; public class enemy1 extends objects { public void act() { Whentospawn(); setLocation(getX(), getY() - 1 ); } public void Whentospawn() { if (Greenfoot.getRandomNumber( 2 ) == 1 ) { } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import greenfoot.*; public class Spawner extends objects { public void act() { spawn(); } public void spawn() { if (Greenfoot.getRandomNumber( 1 ) == 0 ) //every act { getWorld().addObject( new enemy1(), getX(),getY()); } } } |
1 2 | turnTowards(getWorld().getWidth()/ 2 ,getWorld().getHeight()/ 2 ); // turnTowards the centre move(<speed>) |
1 2 3 | enemy1.Whentospawn(); enemy2.Whentospawn(); // etc. |
1 2 3 | enemy1.Whentospawn(); enemy2.Whentospawn(); // etc. |