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

2012/9/6

Creating a method

DPD4AU DPD4AU

2012/9/6

#
if I wanted to create a method named cannotMove, by using if atWorldEdge and canSee(crab.class). How would I go about this so that cannotMove returns true when one or the other is invoked?
danpost danpost

2012/9/6

#
Compare the code below with how you stated what you wanted to do.
public boolean cannotMove()
{
    return (atWorldEdge() || canSee(crab.class));
}
DPD4AU DPD4AU

2012/9/6

#
Awesome, everything is working just fine now! Thanks Dan!
DPD4AU DPD4AU

2012/9/6

#
alright one more question, i need the 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), snd 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.
danpost danpost

2012/9/6

#
I cannot answer your questions at the moment. Will respond to them this evening, if someone else hasn't helped you by then. Best regards.
danpost danpost

2012/9/6

#
Would this work:
private void changeDirection()
{
    turnLeft(); // now facing left
    if (!cannotMove) return;
    turnBack(); // turning from facing left to facing right
    if (!cannotMove) return;
    turnRight(); // turning from facing right to facing back
    if (!cannotMove) return;
    turn(17);
}
You need to login to post a reply.