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

2017/11/20

How do I make 2 Actors, not run into each other, horizontaly and verticaly?

TheShyGuy TheShyGuy

2017/11/20

#
Hi I am making a Turn based program, in which one Actor at a time is walking a distance of 1 cell, if this cell already has an actor on it, the actor should just move in another direction. It's simple... yet it doesn't seem to work for me.
public void travel(int distance)
    {
        int size = getNeighbours(1,false,Actor.class).size();
        boolean d1 = false;
        boolean d2 = false;
        boolean d3 = false;
        boolean d4 = false;
        if(size == 4)
        {
            rotate();
        }
        else
        {
            for(int i=0; i>size; i++)
            {
                Actor x = (Actor)getNeighbours(1,false,Actor.class).get(i);
                if((getX() < x.getX())&& getRotation() == 0)
                {
                    if(d2&&d3)
                    {
                        setRotation(270);
                    }
                    else if(d3&&d4)
                    {
                        setRotation(90);
                    }
                    else if(d2&&d4)
                    {
                        setRotation(180);
                    }
                    else if(d2)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(90*rot+180);
                    }
                    else if(d3)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(180*rot+90);
                    }
                    else if(d4)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(90*rot+90);
                    }
                    else
                    {
                        int rot = Greenfoot.getRandomNumber(3);
                        setRotation(90*rot+90);
                    }
                    d1 = true;
                }
                else if((getY() > x.getY())&& getRotation() == 90)
                {
                    if(d1&&d3)
                    {
                        setRotation(270);
                    }
                    else if(d3&&d4)
                    {
                        setRotation(0);
                    }
                    else if(d1&&d4)
                    {
                        setRotation(180);
                    }
                    else if(d1)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(90*rot+180);
                    }
                    else if(d3)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(270*rot);
                    }
                    else if(d4)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(180*rot);
                    }
                    else
                    {
                        int rot = Greenfoot.getRandomNumber(3);
                        setRotation(-45*rot^2+225*rot);
                    }
                    d2 = true;
                }else if((getX() > x.getX())&& getRotation() == 180)
                {
                    if(d1&&d2)
                    {
                        setRotation(270);
                    }
                    else if(d2&&d4)
                    {
                        setRotation(0);
                    }
                    else if(d1&&d4)
                    {
                        setRotation(90);
                    }
                    else if(d1)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(180*rot+90);
                    }
                    else if(d2)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(270*rot);
                    }
                    else if(d4)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(90*rot);
                    }
                    else
                    {
                        int rot = Greenfoot.getRandomNumber(3);
                        setRotation(45*rot^2+45*rot);
                    }
                    d3 = true;
                }
                else if((getY() < x.getY())&& getRotation() == 270)
                {
                    if(d1&&d2)
                    {
                        setRotation(180);
                    }
                    else if(d2&&d3)
                    {
                        setRotation(0);
                    }
                    else if(d1&&d3)
                    {
                        setRotation(90);
                    }
                    else if(d1)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(90*rot+90);
                    }
                    else if(d2)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(90*rot);
                    }
                    else if(d3)
                    {
                        int rot = Greenfoot.getRandomNumber(2);
                        setRotation(180*rot);
                    }
                    else
                    {
                        int rot = Greenfoot.getRandomNumber(3);
                        setRotation(45*rot^2+45*rot);
                    }
                    d4 = true;
                }
            }
            move(distance);
        }
    } 
This is the Code i've come up with, yet it doesn't really seem to work. Does anyone have any suggestions on how to fix it?
danpost danpost

2017/11/20

#
In line 14, 'i' needs to be less than 'size' (not greater than).
TheShyGuy TheShyGuy

2017/11/20

#
danpost wrote...
In line 14, 'i' needs to be less than 'size' (not greater than).
XD Thank you^^, i didn't see that.
matthijs124 wrote...
You can set up some kind of boolean like "isOccupied" and only move to the certain tile if the tile is NOT isOccupied --> but then you have to determine this BEFORE you move.
Thank you too^^, that gave me a good idea on something :3
You need to login to post a reply.