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

2019/5/15

LittleCrab - No error message but doesn't work

SushiSlurp SushiSlurp

2019/5/15

#
I want to make a Lobster, which chases the Crab that you control with your mouse. To do that, i make the lobster always turn to the coordinates of the Crab. This worked but you couldn´t run from the lobster because it always turned to you instandly. To make it harder for the lobster I tried to delay the turning. When I start the game don´t get an error message but instead of following the crab with some delay the lobster walks to the diagonal line from the top left corner to the bottom right corner. After that it just moves on that diagonal line but still tries to follow the crab. Here the Code:
public class Lobster extends Animal
{
    private int counter;
    private int crabX;
    private int crabY;
    private int crabX0;
    private int crabY0;
    private int crabX1;
    private int crabY1;
    private int crabX2;
    private int crabY2;
    private int crabX3;
    private int crabY3;
    private int crabX4;
    private int crabY4;
    /**
     * Act - do whatever the Lobster wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Lobster(){
        crabX0 = 0;
        crabY0 = 0;
        crabX1 = 0;
        crabY1 = 0;
        crabX2 = 0;
        crabY2 = 0;
        crabX3 = 0;
        crabY3 = 0;
        crabX4 = 0;
        crabY4 = 0;
    }
    public void act() 
    {
        move();
        int newCrabX = ((Crab) getWorld().getObjects(Crab.class).get(0)).getX();
        int newCrabY = ((Crab) getWorld().getObjects(Crab.class).get(0)).getY();
        crabX = newCrabX;
        crabY = newCrabY;
        turnLobster();
        counter++;
    }
    public void turnLobster(){
        switch(counter){
            case(0):
            crabX0 = crabX;
            crabY0 = crabY;
            turnTowards(crabX1, crabX1);
            break;
            case(1):
            crabX1 = crabX;
            crabY1 = crabY;
            turnTowards(crabX2, crabX2);
            break;
            case(2):
            crabX2 = crabX;
            crabY2 = crabY;
            turnTowards(crabX3, crabX3);
            break;
            case(3):
            crabX3 = crabX;
            crabY3 = crabY;
            turnTowards(crabX4, crabX4);
            break;
            case(4):
            crabX4 = crabX;
            crabY4 = crabY;
            turnTowards(crabX0, crabX0);
            counter = -1;
            break;
        }
    }
}
rb6333 rb6333

2019/5/15

#
Try using this code in the act method of the Lobster class to make the lobster chase the crab public void act(){ turnTowards(Crab.getX(),Crab.getY()); move(5); }
SushiSlurp SushiSlurp

2019/5/15

#
that would work but i tried to make a delay so that the lobster always turns to the position the crab was 5 timeunits ago, thats why i made the switch case statement
You need to login to post a reply.