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

2016/5/16

Prevent overlapping

redteam55 redteam55

2016/5/16

#
Is there a way to prevent actors from overlapping? Because right now, I have actors that are following the player, but if the player goes around these actors for long enough, the actors bunch together and merge into what seems like, on the outside, one actor. I saw other posts suggesting turning the actors 180 degrees then moving, but i don't want them to turn. I have already tried
1
2
3
4
5
6
7
private void dontStack()
{
    if(isTouching(Scarecrows.class))
    {
        move(-speed*2);
    }
}
the above works, but at certain times, the two actors will always be touching each other even while moving backwards...
danpost danpost

2016/5/16

#
With what you have tried, either the scarecrow will have moved forward some or backward some. But, sometimes neither is possible without running into another. Sometimes, it will have to just stay where it is until one of the others move away. Maybe try forward; if not, pick a random side and move there; if not, try the other side; if not, try backward. After each failed attempt, move back to original location.
You need to login to post a reply.