I tried to add a snowman that can stand and shoot water as well but when the water of the snowman hits the plant it gives an error.
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;
int healthBar = 100;
int timeLeft = 3000;
/**
* Act: walk around (using arrows), create plants (using "z")
* spray water (using space)
*/
public void act()
{
if (Greenfoot.isKeyDown("a")) {
setRotation(180);
move(5);
}
if (Greenfoot.isKeyDown("w")) {
setRotation(270);
move(5);
}
if (Greenfoot.isKeyDown("d")) {
setRotation(0);
move(5);
}
if (Greenfoot.isKeyDown("s")) {
setRotation(90);
move(5);
}
if (Greenfoot.isKeyDown("f")){
World word = getWorld();
getWorld(World.addObject(new Snowman(), getX(), getY()));
}
// If "z" is pushed, create a plant a the current location
if (Greenfoot.isKeyDown("z")) {
World world = getWorld();
world.addObject(new Plant(), 300, 60);
world.addObject(new Plant(), 200, 100);
world.addObject(new Plant(), 250, 150);
}
// If spacebar is pushed, create a waterspray at the current location
if (Greenfoot.isKeyDown("space")) {
World world = getWorld();
WaterSpray waterSpray = new WaterSpray();
waterSpray.setGardener(this);
world.addObject(waterSpray, getX(), getY());
} 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();
world.addObject(waterSpray, getX(), getY());
}
