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);
}
}
