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

2017/9/14

Need help with crab code. Trying to move a certain way

Pooky Pooky

2017/9/14

#
import greenfoot.*;

/**
 * This class defines a crab. Crabs live on the beach.
 */
public class Crab extends Actor
{
    public void act()
    {
        move(5);
        if (isAtEdge()) {
            backUp(-50);
            turnLeft();
        }
    }
    
    /** Turns the crab to face left from its current perspective. */
    public void turnLeft() {
        turn(-90);
    }
    
    /** Moves the crab backwards the given distance. */
    public void backUp(int distance) {
       move(-50);
    }
}


I am trying to get my crab to go to the wall, back up 50, then turn to the left. He does this, but the problem is that "backUp" is not affected by whatever number I put under the actor class. So if I want him to move backwards 50 in one situation but backwards 150 in another, I can't do that. It only takes "move(-50);" into consideration. How do I get the backUp (int) to actually matter? Just to clarify, even if backUp(2) he still moves 50 to the left. I want him to move whatever amount I put into "backUp" not whatever I put into "move ".
danpost danpost

2017/9/14

#
What you put in move is what will happen. The value of the argument you put in backUp will be the value of the parameter distance. If you want to move distance, then use distance in move.
Pooky Pooky

2017/9/14

#
Thank you!! That totally worked. I had a complete brain fart earlier while trying to work on this.
You need to login to post a reply.