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

2015/3/12

WALKING LOOP!? Please Help!

Prish950 Prish950

2015/3/12

#
Hey we want a Person to walk in a loop to the bottom of the map and than back to the place where he has been spawned. The Problem is the spawn place is always different. We wrote:
setRotation(180);
        move(2);
        move(2);
        move(2);
        setRotation(0);
        move(2);
        move(2);
        move(2);
we wrote this but it doesn't worked.
Super_Hippo Super_Hippo

2015/3/12

#
You could save the y-coordinate when you are spawned (use the 'addedToWorld' method). Then you move down until you reach the bottom and after that going up until you reach your old position and so on.
private int yStart = 0;
private boolean goingDown = true;

protected void addedToWorld(World w)
{
    yStart = getY();
}

public void act()
{
    if (goingDown)
    {
        setLocation(getX(), getY()+2)
        if (getY() > getWorld().getHeight()-3) goingDown = false;
    }
    else
    {
        setLocation(getX(), getY()-2)
        if (getY() == yStart) goingDown = true;
    }
}
Prish950 Prish950

2015/3/12

#
Thank You that worked ;) awesome!!
You need to login to post a reply.