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

2019/12/12

how to prevent overlapping

Bigbobatesloppyjoe Bigbobatesloppyjoe

2019/12/12

#
I'm a new coder and I've been creating a game where a alien crosses a road and to the other side to get to the mother ship without being hit by a car the problem is that the cars keep overlapping each other and don't know how to fix this. here's the code
public void act() {
       if (Greenfoot.getRandomNumber(100) == 1){
           addObject(new Car(),0,Greenfoot.getRandomNumber(200)+200);
        }
    }
    
this is in the world class
danpost danpost

2019/12/12

#
Add new cars into the world at (0, 0) and use the addedToWorld method in the Car class to place the car so that it is not touching another:
protected void addedToWorld(World world)
{
    while (getY == 0 || isTouching(Car.class)) setLocation(0, Greenfoot.getRandomNumber(200)+200);
}
Bigbobatesloppyjoe Bigbobatesloppyjoe

2019/12/13

#
What should I add into the (World world) area? My world name is Road and I tried Road, Road.class, and Road(). But, it keeps giving me errors.
Super_Hippo Super_Hippo

2019/12/13

#
You keep it as it is. At least the “World”. You can change “world” to whatever variable name you want. It isn’t used anyway.
Bigbobatesloppyjoe Bigbobatesloppyjoe

2019/12/13

#
Ok it works thanks!
You need to login to post a reply.