Hello,
For a project i'm making i'm currently trying to let the enemies spawn at random locations. But letting them spawn via a contructor in the world class i can only make them not spawn exactly on top of each other. But not completly apart from one another. Wich would be fixed if i could just use the isTouching(i think). Since they need to spawn in a certain area of the world i am using x and y values for that. However there are other objects, like walls and they cannot spawn on them aswell. I tried making a list with Actor.class instead of Jock.class but that didnt work either, as the constructor could still place them slighty off center from the walls. This has resulted in 3 sets of code within one method where i think i can make it 1 set of code. (losing the o and u integers). The code is below. Any help is appreciated.
/** * Spawns 6 jocks at random locations within the 3 hallways of the school */ private void createJocks() { int i = 0; int o = 0; int u = 0; while (i < 2) { Jock jock = new Jock(); int x = 245 + Greenfoot.getRandomNumber(455); int y = 310 + Greenfoot.getRandomNumber(50); boolean spaceTaken = false; List<Jock> jocks = (List<Jock>)getObjects(Jock.class); for (Jock evilJocks : jocks) { if (evilJocks.getX()== x && evilJocks.getY()== y) spaceTaken = true; } if (!spaceTaken) addObject(jock, x ,y ); i++; } while (o < 2) { Jock jock = new Jock(); int x = 245 + Greenfoot.getRandomNumber(455); int y = 410 + Greenfoot.getRandomNumber(50); boolean spaceTaken = false; List<Jock> jocks = (List<Jock>)getObjects(Jock.class); for (Jock evilJocks : jocks) { if (evilJocks.getX()== x && evilJocks.getY()== y) spaceTaken = true; } if (!spaceTaken) addObject(jock, x ,y ); o++; } while (u < 2) { Jock jock = new Jock(); int x = 245 + Greenfoot.getRandomNumber(455); int y = 510 + Greenfoot.getRandomNumber(50); boolean spaceTaken = false; List<Jock> jocks = (List<Jock>)getObjects(Jock.class); for (Jock evilJocks : jocks) { if (evilJocks.getX()== x && evilJocks.getY()== y) spaceTaken = true; } if (!spaceTaken) addObject(jock, x ,y ); u++; } }