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

2016/12/20

Random Spawning help

MalumKappa MalumKappa

2016/12/20

#
So, I'm making a game where enemies will approach you as they spawn, but my current code does not spawn anything.
public CrabWorld()
    {    
        super(600, 400, 1); 
        randomSpawner();
        prepare();
        
}
public void randomSpawner()
{

int y = Greenfoot.getRandomNumber(getHeight());
 
Actor food = null;
switch (Greenfoot.getRandomNumber(3))
{
case 0: food = new Lobster(); break;
case 1: food = new Lobster(); break;
case 2: food = new Lobster(); break;
 

}
if (Greenfoot.getRandomNumber(100) <= 10)
{
addObject(food,500,y);
}

}
It just spawns on lobster(or none), then it stops; how to fix this?
danpost danpost

2016/12/20

#
You are calling 'randomSpawner' from the constructor of the world. The world is only constructed once, so the method is only called once. You need to call the method from the 'act' method which greenfoot calls repeatedly whilst the scenario runs.
MalumKappa MalumKappa

2016/12/20

#
Thanks
You need to login to post a reply.