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

2012/9/6

One Method all together

DPD4AU DPD4AU

2012/9/6

#
I need a crab to turn to the left while moving. I've already created methods for turnLeft (which is turn -90), turnRight (which is turn 90), turnBack (and turn180), and the cannotMove method you helped me with. I now need a changeDirecction method that naturally turns left while moving, if it cannot move, it turns right, if it still cannot move, it turns back, if it still cannot move it turns 17 degrees, how would i combone all these methods i have created to form one big method.
Zamoht Zamoht

2012/9/6

#
if (cannotMoveLeft)
{
     if (cannotMoveRight)
     {
          if(cannotMoveBackwards)
          {
               setRotation(getRotation() + 17);
          }
          else
          {
                setRotation(getRotation() + 180);
          }
     }
     else
     {
           setRotation(getRotation() + 90);
     }
}
else
{
     setRotation(getRotation() - 90);
}
I don't know if that answers your question, but if I get your question right you need to declare the cannotMove<Direction> booleans and from there it should work.
Gevater_Tod4711 Gevater_Tod4711

2012/9/6

#
I would use another mehtod. I don't think this acts like you want.
public void changeDirection() {
    if (cannotMove()) {
        turnRight();
        if (cannotMove()) {
            turnBack();
        }
        if (cannotMove()) {
            turn(17);
        }
    }
    else {
        turnLeft();
    }
}
and @ Zamoht there is a method for this: setRotation(getRotation() +- angle) try to use the turn method. It probably is simpler and doe's the same.
danpost danpost

2012/9/6

#
See my response in the discussion Creating a method.
You need to login to post a reply.