Hello, I'm not sure why this code does not work properly as it seems that the method that I use here makes complete sense to me. I want to prevent my roach objects from spawning on the car objects and I don't understand why this code that I have come up with doesn't do the trick. The method for some reason sometimes works while sometimes it doesn't, and some roaches spawn directly on the car object.
Here's my code:
and that's what sometimes happens:
I would highly appreciate any kind of help along with an explanation,
thanks!
public boolean getReady() { readyToStart = false; do { if(this.isAtEdge() && !this.getIntersectingObjects(Car.class).isEmpty()) { int newRandX = 1 + Greenfoot.getRandomNumber(getWorld().getWidth()); int newRandY = 1 + Greenfoot.getRandomNumber(getWorld().getHeight()); this.setLocation(newRandX, newRandY); System.out.println("problem fixed"); } else { readyToStart = true; } } while(!readyToStart); //Returning whether overlapping had occurred. False stands for yes, //while true for no. return readyToStart; }