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

2018/6/21

Why am I getting an error?

1
2
3
Lukaas Lukaas

2018/6/21

#
I am sorry I feel dumb now but I really dont understand
danpost danpost

2018/6/21

#
Lukaas wrote...
??
danpost wrote...
WaterSpray waterSpray = new WaterSpray(this);
Lukaas Lukaas

2018/6/21

#
where do I have to put that code??
danpost danpost

2018/6/21

#
Lukaas wrote...
where do I have to put that code??
Lukaas wrote...
There is still an error in the snowman class, line 128.
Lukaas Lukaas

2018/6/21

#
It says incompatible types: Snowman cannot be converted to Gardener
Lukaas Lukaas

2018/6/21

#
import greenfoot.*;
import java.awt.Color;

/**
 * A waterspray can be sprayed by the gardener. It helps to give plants water.
 * 
 */
public class Snowman extends Actor
{   
    // Field with an objectreference to the gardener that sprayed this water
    Gardener gardener;
   int setSnowman;
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
   
   
     
    public void act () {
        move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
                gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
       }
 move(0);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
        gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
    }
   if (Greenfoot.isKeyDown("space")) {
        World world = getWorld();
        WaterSpray waterSpray = new WaterSpray(this);
        world.addObject(waterSpray, getX(), getY());
    }
}
}
    
      
     
    

danpost danpost

2018/6/21

#
Sorry, I had no idea you were creating WaterSpray objects from two totally different classes. Remove the this from the WaterSpray constructor calls in both Snowman and Gardener classes and remove the constructor from the WaterSpray class. Make the gardener field in the WaterSpray class static:
public static Gardener gardener;
and add the following to the Gardener class,:
public Gardener()
{
    WaterSpray.gardener = this;
}
Lukaas Lukaas

2018/6/21

#
When I type: public static Gardener gardener; It says variable gardener is already defined in class WaterSpray
danpost danpost

2018/6/21

#
Lukaas wrote...
When I type: public static Gardener gardener; It says variable gardener is already defined in class WaterSpray
You were supposed to change a line (line 11 in the WaterSpray class) -- not add a new one.
Lukaas Lukaas

2018/6/21

#
Maybe because I already have the code in my Gardener class?
Lukaas Lukaas

2018/6/21

#
import greenfoot.*;
import java.awt.Color;

/**
 * A waterspray can be sprayed by the gardener. It helps to give plants water.
 * 
 */
public class WaterSpray extends Actor
{   
    // Field with an objectreference to the gardener that sprayed this water
   public static Gardener gardener;

   
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
    public void Gardener(){ 
    {
        gardener = g;
    }    
    
    
}public void act () {
        move(3);
        if (isTouching (Plant.class)){
        Plant plant = (Plant)getOneIntersectingObject (Plant.class);
        plant.addWater(1);
        gardener.addPoints(1);
        World world = getWorld();
        world.removeObject (this);
    }
} 
    }
What do I have to change still? My variable G cannot be found
Lukaas Lukaas

2018/6/21

#
nevermind man, thx for you help. It works!!
Lukaas Lukaas

2018/6/21

#
Is there a code that you can only place a maximum of 1 snowman for example??
danpost danpost

2018/6/21

#
Lukaas wrote...
Is there a code that you can only place a maximum of 1 snowman for example??
If adding from your World subclass act method:
if (getObjects(Snowman.class).isEmpty()) addObject(new Snowman());
Lukaas Lukaas

2018/6/21

#
import greenfoot.*;

/**
 * Within the GreeneryWorld there walks a gardener to water his plants.
 * 
 */
public class GreeneryWorld extends World
{

    /**
     * Constructor for objects of class GreeneryWorld.
     * 
     */
     public void act(){
            if (getObjects(Snowman.class).isEmpty()) 
            addObject(new Snowman());
            }
    public GreeneryWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        // Objects can move outside the edges of the world
        super(600, 400, 1, false); 
        // Create a new gardener and put in the world
        addObject (new Gardener(), 300,200);
        // Create a new timer and put it in the upper left corner
        addObject (new Timer(),25,15);
    }
        }


blic GreeneryWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        // Objects can move outside the edges of the world
        super(600, 400, 1, false); 
        // Create a new gardener and put in the world
        addObject (new Gardener(), 300,200);
        // Create a new timer and put it in the upper left corner
        addObject (new Timer(),25,15);}
        {
        if (getObjects(Snowman.class).isEmpty())
             addObject(new Snowman());
            }   
            }
    
This is my world now the addObject in line 16 doesn't work
There are more replies on the next page.
1
2
3