Home » Java » java – How to stop actor from continuously picking up object?-Exceptionshub

java – How to stop actor from continuously picking up object?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m taking an intro to java class at school and were doing very early on stuff. We are given a few methods and the knowledge of how if else statements, while loops, etc work. We use a program called greenfoot. The objective of the project is to have the character go around and pick up all the leaves, then place them at the end of the row and repeat. The order is changed so you need control statements. I am encountering an issue where at the end of each row, I will place a leaf, then pick it up and loop that forever. I have tried to solve it with variables to no avail. My code is here:

public void rowSweep() {
        boolean turnCheck = false;
        boolean leafCheck = true;
        if (foundLeaf() && (leafCheck)) {
            pickLeaf();
            leafCheck = true;
        }
        else if (canMove() && (!facingNorth())) {
            walk();
            leafCheck = true;
        }
        else if (hasLeaf()) {
            while (hasLeaf()) {
                placeLeaf();
                leafCheck = false;
            }
        }
        else {
            turnLeft();
            while (canMove() && (!facingNorth())) {
                walk();
                turnCheck = true;
            }
            if (!canMove() && (turnCheck)) {
                turnLeft();
                walk();
                turnLeft();
            }
        }
    }

There are no syntax errors, just it doesn’t do it properly. How can I make it so that I can move on after I place it instead of infinitely repeating?

How to&Answers: