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

2018/12/22

intersecting objects

sarah02 sarah02

2018/12/22

#
Hi. I am adding objects into the world as soon as the game is run but most times they intersect, how would i stop that?
Super_Hippo Super_Hippo

2018/12/22

#
You can check if it intersects after it was added to the world and if it does, relocate them randomly until a free location is found.
protected void addedToWorld(World world)
{
    while (isTouching(<ClassName>.class))
    {
        setLocation(<random-x>, <random-y>);
    }
}
sarah02 sarah02

2018/12/23

#
Thank u!
You need to login to post a reply.