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

2017/1/17

AddPoints

dendepen dendepen

2017/1/17

#
I want to add point to the gardener if he gives the plant water
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
    Gardener gardener;
    
    /**
     * Move forward, not to slowly
     */
    public void act() {
        move(3);
        if (isTouching (Plant.class)) {
            Plant plant = (Plant)getOneIntersectingObject(Plant.class);
            plant.addWater(1);
            World world = getWorld();
            world.removeObject (this);
            gardener.addPoints(+1);
        } 
    } 
    
    /**
     * Set the objectreference to the gardener
     * @param g The objectreference to the gardener
     */
    public void setGardener (Gardener g) {
        gardener = g;
    }    
}
______________________________________
import greenfoot.*;
import java.awt.Color;

/**
 * A gardener walks around and sprays water for his plants. It's a game, so it 
 * keep tracks of the amount of points. The more points, the better of course.
 * 
 */
public class Gardener extends Actor
{
    // Field for keeping track of the points
    int points = 0;
    
    /**
     * Act: walk around (using arrows), create plants (using "z")
     * spray water (using space)
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("left")) {
            setRotation(180);
            move(1);
        }
        if (Greenfoot.isKeyDown("up")) {
            setRotation(270);
            move(1);
        }
        if (Greenfoot.isKeyDown("right")) {
            setRotation(0);
            move(1);
        }
        if (Greenfoot.isKeyDown("down")) {
            setRotation(90);
            move(1);
        }        
        
        // If "z" is pushed, create a plant a the current location
        if (Greenfoot.isKeyDown("z")) {
            World world = getWorld();
            world.addObject(new Plant(), getX(), getY());
        }   
        
        // If spacebar is pushed, create a waterspray at the current location
        if (Greenfoot.isKeyDown("space")) {
            World world = getWorld();
            world.addObject(new WaterSpray(), getX(), getY());
        }
       
        // Update the points
        updateText();
    }    
    
    /**
     * Add a certain amount of points (e.g. when the waterspray hits a plant)
     * Make sure the gardener shows the amount of points
     * @param p amount of points to be added
     */
    public void addPoints (int p) {
        points += p;
        updateText();
    }
    
    /**
     * Show the amount of points above the head of the gardener. This is a non-public
     * method (only avaible from inside the class).
     * 
     */
    private void updateText() {
        GreenfootImage newImage = new GreenfootImage ("gardener.png");      
        newImage.setColor (Color.BLACK);
        newImage.drawString(points + "", 10, 15);
        setImage (newImage);
    }
}
danpost danpost

2017/1/17

#
You cannot set the Gardener field of the SprayWater object when you do not have a reference to the object. Do not create the SprayWater object in the 'addObect' line -- create it before the line, set the Gardener object reference in it and then add it to the world:
// replace line 46 with the following
SprayWater spray = new SprayWater();
spray.setGardener(this);
world.addObject(spray, getX(), getY());
You need to login to post a reply.