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

2019/5/14

LittleCrab

SushiSlurp SushiSlurp

2019/5/14

#
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. Here the Code that doesn´t work:
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);
            case(1):
            crabX1 = crabX;
            crabY1 = crabY;
            turnTowards(crabX2, crabX2);
            case(2):
            crabX2 = crabX;
            crabY2 = crabY;
            turnTowards(crabX3, crabX3);
            case(3):
            crabX3 = crabX;
            crabY3 = crabY;
            turnTowards(crabX4, crabX4);
            case(4):
            crabX4 = crabX;
            crabY4 = crabY;
            turnTowards(crabX0, crabX0);
            counter = -1;
            break;
        }
    }
}
Super_Hippo Super_Hippo

2019/5/14

#
Rule number 1: "Doesn't work" is not specific at all. You should say if (and if yes where) you get an error while compiling or while running the game. And if there is no error, you need to tell what exactly happens instead of what should happen. In your case, your switch structure is wrong.
switch (counter)
{
    case 0:
    //code for case 0
    break;
    
    case 1:
    //code for case 1
    break;
    
    //...
}
SushiSlurp SushiSlurp

2019/5/15

#
I 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.
danpost danpost

2019/5/15

#
All your turnToward calls use two x-coordinate values. You need to use one x- and one y-coordinate.
You need to login to post a reply.