Hello to all,
I want to place ten Objects randomly in a world. The Objects should not intersect. This is my code:
It does not work. Often the bugs intersect with other bugs. If I replace the random number generation with a numeral, the bugs does not intersect anymore, but the place is no longer randomly.
What is the Problem?
Thank you very much!
Greetings
Diphthong
EDIT: This code is in the world class, so I can’t use Actors methods.
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
for(int i = 0; i < 10; i++)
{
int x = (Greenfoot.getRandomNumber(560)+20);
int y = (Greenfoot.getRandomNumber(360)+20);
while(!getObjectsAt(x, y, bug.class).isEmpty())
{
x += 20;
y += 20;
}
addObject(new bug(), x, y);
}
