ok, so i have 2 classes for my td for health. One removes spawns when it sees them (like the eat method of crab), and then decrements the life counter by one (amount of spawns that can leave before game over). The other class is suppose to take the value from the previous class and display it in green-foot, but it won't change the number after a spawn gets through. It only takes the value from the first class once and doesn't update it. here's the coding for both and the world:
world coding:
coding for displaying:
coding for the point where it decrements:
public tdpath() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); defenseSpot d = new defenseSpot(); addObject(new spawning(), 35, 100); addObject(new defenseSpot(), 100, 580); //addObject(new uiMenu(), 150, 550); //addObject(new uiMenu(u.update()), 150, 500); addObject(new LivesLeft(d.health()), 150, 550); }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class LivesLeft here. * * @author (your name) * @version (a version number or a date) */ public class LivesLeft extends Actor { /** * Act - do whatever the LivesLeft wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int hp = 0; private int hpBase; public LivesLeft(int health) { healthLeft(health); hpBase = health; setImage(new GreenfootImage("" + hp, 20, Color.WHITE, Color.BLACK)); } public LivesLeft() { healthLeft(hp); setImage(new GreenfootImage("" + hp, 20, Color.WHITE, Color.BLACK)); } public void act() { // Add your action code here. healthLeft(hpBase); LifePic(); } private void healthLeft(int health) { hp = health; } private void LifePic() { healthLeft(hp); setImage(new GreenfootImage("" + hp, 20, Color.WHITE, Color.BLACK)); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class defenseSpot here. * * @author (your name) * @version (a version number or a date) */ public class defenseSpot extends Actor { /** * Act - do whatever the defenseSpot wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public int totalHealth = 20; public void act() { // Add your action code here. health(); eat(); } private void eat() { Actor unit; unit = getOneObjectAtOffset(0, 0, enemies.class); if (unit != null) { World world; world = getWorld(); world.removeObject(unit); totalHealth--; } if (totalHealth <= 0) { ((tdpath)getWorld()).addObject(new gameOver(), 400, 200); Greenfoot.stop(); } } public int health()//void health()//int health() { //getImage().drawString("Lives Left: " + totalHealth, 150, 550); //setImage(new GreenfootImage("Lives Left: " + totalHealth, 20, Color.WHITE, Color.BLACK)); //GreenfootImage life = new GreenfootImage("Lives Left: ", 20, Color.WHITE, Color.BLACK); //life.drawString("Lives Left: " + totalHealth, 150, 550); //GreenfootImage.drawString("Lives Left: " + totalHealth, 150, 550); return totalHealth; } }