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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | /** * 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++; } } |