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

2022/1/2

Error at getWorld().addObject(new Tail( r, g, b)); help

4ldRg 4ldRg

2022/1/2

#
int r, g, b, player;
    int speed = 3;
    int count = 0;
    public Player(int player,int r, int g, int b)
    {
        setRotation(270);
        this.r = r;
        this.g = g;
        this.b = b;
        this.player = player;
        getImage().setColor(new Color(r,g,b));
        getImage().fillRect(0,0,40,40);
    }
    
    public void act()
    {
        count++;
        getWorld().addObject(new Tail( r, g, b))
        move(speed);
        moveAround();
        eatFood();
    }
    public void moveAround()
    {
        if(this.player == 0){
            if(Greenfoot.isKeyDown("right"))
                setRotation(0);
            if(Greenfoot.isKeyDown("left"))
                setRotation(180);
            if(Greenfoot.isKeyDown("up"))
                setRotation(270);
            if(Greenfoot.isKeyDown("down"))
                setRotation(90);
        }
        if(this.player == 4){
            if(Greenfoot.isKeyDown("d"))
                setRotation(0);
            if(Greenfoot.isKeyDown("a"))
                setRotation(180);
            if(Greenfoot.isKeyDown("w"))
                setRotation(270);
            if(Greenfoot.isKeyDown("s"))
                setRotation(90);
        }
        // Add your action code here.
    }

    public void eatFood()
    {
        if(isTouching(Food.class) && player == 0)
        {  
           MyWorld myWorld = (MyWorld) getWorld();
           myWorld.blueCounter.addScore();    
        }
        if(isTouching(Food.class) && player == 4)
        {  
           MyWorld myWorld = (MyWorld) getWorld();
           myWorld.greenCounter.addScore();    
        }
    }
}
Spock47 Spock47

2022/1/3

#
4ldRg wrote...
        getWorld().addObject(new Tail( r, g, b))
The semicolon at the end of the line is missing:
        getWorld().addObject(new Tail( r, g, b));
Live long and prosper, Spock47
danpost danpost

2022/1/3

#
Spock47 wrote...
The semicolon at the end of the line is missing:
Also missing are the x and y coordinates where the actor is to be placed in the world:
getWorld().addObject(new Tail(r, g, b), getX(), getY());
4ldRg 4ldRg

2022/1/3

#
still does not work:
int r, g, b, player;
    int speed = 3;
    int count = 0;
    public Player(int player,int r, int g, int b)
    {
        setRotation(270);
        this.r = r;
        this.g = g;
        this.b = b;
        this.player = player;
        getImage().setColor(new Color(r,g,b));
        getImage().fillRect(0,0,40,40);
    }
    
    public void act()
    {
        count++;
        getWorld().addObject(new Tail(r, g, b), getX(), getY());
        move(speed);
        moveAround();
        eatFood();
    }
    public void moveAround()
    {
        if(this.player == 0){
            if(Greenfoot.isKeyDown("right"))
                setRotation(0);
            if(Greenfoot.isKeyDown("left"))
                setRotation(180);
            if(Greenfoot.isKeyDown("up"))
                setRotation(270);
            if(Greenfoot.isKeyDown("down"))
                setRotation(90);
        }
        if(this.player == 4){
            if(Greenfoot.isKeyDown("d"))
                setRotation(0);
            if(Greenfoot.isKeyDown("a"))
                setRotation(180);
            if(Greenfoot.isKeyDown("w"))
                setRotation(270);
            if(Greenfoot.isKeyDown("s"))
                setRotation(90);
        }
        // Add your action code here.
    }

    public void eatFood()
    {
        if(isTouching(Food.class) && player == 0)
        {  
           MyWorld myWorld = (MyWorld) getWorld();
           myWorld.blueCounter.addScore();    
        }
        if(isTouching(Food.class) && player == 4)
        {  
           MyWorld myWorld = (MyWorld) getWorld();
           myWorld.greenCounter.addScore();    
        }
    }
}
4ldRg 4ldRg

2022/1/3

#
danpost wrote...
Spock47 wrote...
The semicolon at the end of the line is missing:
Also missing are the x and y coordinates where the actor is to be placed in the world:
getWorld().addObject(new Tail(r, g, b), getX(), getY());
can you help me?
danpost danpost

2022/1/3

#
4ldRg wrote...
can you help me?
???
Spock47 Spock47

2022/1/3

#
4ldRg wrote...
can you help me?
Please give 1. the source code of class Tail and 2. the error message that the compiler gives you (or a clear description of what "does not work" mean). Also, the given source code of class Player misses the first line. Live long and prosper, Spock47
4ldRg 4ldRg

2022/1/3

#
code of class tail :
int r, g, b, player;
    int countLength = 0;
    static int blueLength = 1;
    static int greenLength = 1;
    public Tail(int player,int r, int g, int b)
    {
        this.r = r;
        this.g = g;
        this.b = b;
        getImage().setColor(new Color(r,g,b));
        getImage().fillRect(0,0,40,40);
    }
    public void act()
    {
        
        countLength++;
        
        if(countLength>15 && isTouching(Player.class))
        {
            getWorld().addObject(new YouLose(), getWorld().getWidth()/2, getWorld().getHeight()/2);
        }
        if(player == 4 && countLength % greenLength == 0){
            getWorld().removeObject(this);
            greenLength++;
        }
        
        if (player == 0 && countLength % blueLength == 0){
         getWorld().removeObject(this);
         blueLength++;
        }
The error message is: " constructor Tail in class Tail cannot be applied to given types; required: int,int,int,int found: int,int,int reason: actual and formal argument lists differ in length also code for class player again:
 int r, g, b, player;
    int speed = 3;
    int count = 0;
    public Player(int player,int r, int g, int b)
    {
        setRotation(270);
        this.r = r;
        this.g = g;
        this.b = b;
        this.player = player;
        getImage().setColor(new Color(r,g,b));
        getImage().fillRect(0,0,40,40);
    }
    
    public void act()
    {
        count++;
        getWorld().addObject(new Tail(r, g, b), getX(),getY());
        move(speed);
        moveAround();
        eatFood();
    }
    public void moveAround()
    {
        if(this.player == 0){
            if(Greenfoot.isKeyDown("right"))
                setRotation(0);
            if(Greenfoot.isKeyDown("left"))
                setRotation(180);
            if(Greenfoot.isKeyDown("up"))
                setRotation(270);
            if(Greenfoot.isKeyDown("down"))
                setRotation(90);
        }
        if(this.player == 4){
            if(Greenfoot.isKeyDown("d"))
                setRotation(0);
            if(Greenfoot.isKeyDown("a"))
                setRotation(180);
            if(Greenfoot.isKeyDown("w"))
                setRotation(270);
            if(Greenfoot.isKeyDown("s"))
                setRotation(90);
        }
        // Add your action code here.
    }

    public void eatFood()
    {
        if(isTouching(Food.class) && player == 0)
        {  
           MyWorld myWorld = (MyWorld) getWorld();
           myWorld.blueCounter.addScore();    
        }
        if(isTouching(Food.class) && player == 4)
        {  
           MyWorld myWorld = (MyWorld) getWorld();
           myWorld.greenCounter.addScore();    
        }
    }
}
Spock47 Spock47

2022/1/3

#
4ldRg wrote...
The error message is: " constructor Tail in class Tail cannot be applied to given types; required: int,int,int,int found: int,int,int reason: actual and formal argument lists differ in length
Well, there you have it: it says, that you have to give 4 arguments, but you only give three arguments. You give "r, g, b", but it needs "player, r, g, b". Note: Again, the presented source codes aren't complete (the start for class Player is missing, the start and end of class Tale is missing). Live long and prosper, Spock47
danpost danpost

2022/1/3

#
4ldRg wrote...
code of class tail << Code Omitted >>
Ah. Try:
getWorld().addObject(new Tail(player, r, g, b), getX(), getY())
You need to login to post a reply.