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

2018/9/17

Spawn objects faster the less objects of that kind are in the world

Recorsi Recorsi

2018/9/17

#
Hello, i have a method that spawns rocks over time:
public void placeRocks()
    {
        if(Greenfoot.getRandomNumber(70) == 1)
        {
          int x = Greenfoot.getRandomNumber(900);
          int y = Greenfoot.getRandomNumber(900);
          Rock rock = new Rock();
          rock.setRotation(Greenfoot.getRandomNumber(360));
          addObject(rock, x, y);
        }
    }
Those rocks can be destroyed, what makes the world pretty empty after a while (if you destroy them fast). So i was asking how do i let additional rocks spawn faster the less rocks there are currently in the world? Thanks :)
danpost danpost

2018/9/17

#
You can base the random chance to spawn on the number of rocks currently in the world. For example, line 3 could be:
if (1+Greenfoot.getRandomNumber(100*getObjects(Rock.class).size()) == 1)
Recorsi Recorsi

2018/9/19

#
Thanks Now i get this weird error sometimes: "java.lang.IllegalArgumentException: bound must be positive at Space.placeRocks(Space.java:195) (which is the new code)" and in average rocks spawn a lot less. (Do I have to change to 100 for that?)
Super_Hippo Super_Hippo

2018/9/19

#
Try this:
if (Greenfoot.getRandomNumber(1+50*getObjects(Rock.class).size()) == 0)
Recorsi Recorsi

2018/9/19

#
Super_Hippo wrote...
Try this:
if (Greenfoot.getRandomNumber(1+50*getObjects(Rock.class).size()) == 0)
Thanks :)
You need to login to post a reply.