I am making a plants vs zombies project for my class, and I don't know what I changed in my code, but I can no longer place either my Sunflowers or my Wallnuts. I will paste the code for the world, abstract plant class, wallnut, and sunflower below:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lawn here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lawn extends World
{
private int PeaTimer;
private int zombTimer;
private Location grid;
/**
* Constructor for objects of class Lawn.
*
*/
public Lawn()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1050, 600, 1);
grid = new Location;
for(int r = 0; r < grid.length; r++)
{
for(int c = 0; c < grid.length; c++)
{
grid = new Location(215 + (c*80), 130 + (r*97));
}
}
Menu menu = new Menu();
addObject(menu, getWidth()/2, 38);
menuLoad();
zombTimer=300;
PeaTimer=0;
}
public void act()
{
if(!(StartScreen.checkChange()))
{
Greenfoot.setWorld(new StartScreen());
}
zombSpawn();
if(Greenfoot.mousePressed(this))
{
build(Greenfoot.getMouseInfo().getX(), Greenfoot.getMouseInfo().getY());
}
PeaTimer++;
}
public void build(int x, int y)
{
String key = getKey();
for(int r=0; r < grid.length; r++)
{
for(int c = 0; c <grid.length; c++)
{
if( x + 40 >= grid.getX() && x - 40 <= grid.getX())
{
if(y + 48 >= grid.getY() && y - 48 <= grid.getY())
{
if(checkEmpty(grid.getX(), grid.getY()) && key != null)
{
spawn(grid.getX(), grid.getY(), key);
}
}
}
}
}
}
public String getKey()
{
if(Greenfoot.isKeyDown("P"))
{
return "P";
}
else if(Greenfoot.isKeyDown("S"))
{
return "S";
}
else if(Greenfoot.isKeyDown("W"))
{
return "W";
}
else if(Greenfoot.isKeyDown("M"))
{
return "M";
}
else
{
return null;
}
}
public void spawn(int x, int y, String str)
{
if(str.equals("P")) //&& SunScoreboard.getSun() >= PeaShooter.getCost())
{
addObject(new PeaShooter(), x, y);
SunScoreboard.changeSun((PeaShooter.getCost())*-1);
}
else if(str.equals("S")) //&& SunScoreboard.getSun() >= Sunflower.getCost())
{
addObject(new Sunflower(), x, y);
SunScoreboard.changeSun((Sunflower.getCost())*-1);
}
else if(str.equals("W"))
{
addObject(new Wallnut(), x, y);
SunScoreboard.changeSun((Wallnut.getCost())*-1);
}
/*
else if(str.equals("M"))
{
addObject(new PotatoMine(), x, y);
SunScoreboard.changeSun((PotatoMine.getCost())*-1);
}
*/
else;
//addObject(new PeaShooter(), x, y);
}
public boolean checkEmpty(int x, int y)
{
if(getObjectsAt(x, y, Object.class) instanceof Plant)
{
return false;
}
else
{
return true;
}
}
public void zombSpawn()
{
int zomb = (int)(Math.random()*2);
if(zombTimer == 0)
{
if(zomb == 0)
{
addObject(new RegZombie(), getWidth() , 130 + ((int)(Math.random()*5)*97));
}
else if(zomb == 1)
{
addObject(new BucketZombie(), getWidth(), 130 + ((int)(Math.random()*5)*97));
}
zombTimer=300;
}
else
{
zombTimer--;
}
}
public void menuLoad()
{
BuySunFlower buySun = new BuySunFlower();
addObject(buySun, 313, 38);
BuyPeaShooter buyPea = new BuyPeaShooter();
addObject(buyPea, 407, 38);
BuyWallnut buyWall = new BuyWallnut();
addObject(buyWall, 360, 38);
}
public static void gameOver()
{
Greenfoot.setWorld(new GameOver());
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Plant here.
*
* @author (your name)
* @version (a version number or a date)
*/
public abstract class Plant extends Actor
{
private int cost;
private int health;
private int hitCounter;
/**
* Act - do whatever the Plant wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
}
public boolean checkDamage()
{
if(this.isTouching(Zombie.class) && hitCounter <= 0)
{
return true;
}
return false;
}
public void loseHP()
{
this.health--;
hitCounter = 50;
}
public boolean checkDeath()
{
if(health <= 0)
{
return true;
}
return false;
}
public void die()
{
getWorld().removeObject(this);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Wallnut here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Wallnut extends Plant
{
private GifImage gif;
private boolean changed1;
private boolean changed2;
private int health = 30;
private static int cost = 50;
private int hitCounter;
public Wallnut()
{
gif = new GifImage("WallNut.gif");
hitCounter = 0;
}
/**
* Act - do whatever the Wallnut wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setImage(gif.getCurrentImage());
if(health <= 20 && health > 10)
{
medHealth();
}
if(health <= 10)
{
lowHealth();
}
if(checkDamage() && health > 0)
{
this.loseHP();
}
else
{
this.hitCounter--;
}
if(checkDeath())
{
die();
}
}
public boolean checkDamage()
{
return super.checkDamage();
}
public void loseHP()
{
super.loseHP();
}
public void medHealth()
{
if(changed1 == false)
{
gif = new GifImage("Wallnut_cracked1.gif");
changed1 = true;
}
}
public void lowHealth()
{
if(changed2 == false)
{
gif = new GifImage("Wallnut_cracked2.gif");
changed2 = true;
}
}
public boolean checkDeath()
{
return super.checkDeath();
}
public void die()
{
super.die();
}
public static int getCost()
{
return cost;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Sunflower here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Sunflower extends Plant
{
private static int placeTimer;
private int health = 6;
private int sunTimer=200;
private static int cost = 50;
private GreenfootImage images;
private int currentImage = 0;
private int tmp=15;
private GifImage gif;
private int hitCounter;
public Sunflower()
{
gif = new GifImage("Sunflower.gif");
hitCounter = 0;
}
/**
* Act - do whatever the Sunflower wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
sunSpawn();
setImage(gif.getCurrentImage());
if(checkDamage() && health > 0)
{
this.loseHP();
}
else
{
this.hitCounter--;
}
if(checkDeath())
{
die();
}
}
public boolean checkDamage()
{
return super.checkDamage();
}
public void loseHP()
{
super.loseHP();
}
public boolean checkDeath()
{
return super.checkDeath();
}
public void die()
{
super.die();
}
public void sunSpawn()
{
if(sunTimer<=0)
{
getWorld().addObject(new Sun(), this.getX() + 35, this.getY() - 25);
sunTimer=400;
}
else
{
sunTimer--;
}
}
public static int getCost()
{
return cost;
}
}
The error is that I can no longer place Wallnuts or Sunflowers, if anyone could figure out why, I appreciate the help.
Thanks,
~Calzone

