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

2013/6/25

life counter

darkmist002 darkmist002

2013/6/25

#
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:
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);
    }
coding for displaying:
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));
    }
}
coding for the point where it decrements:
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;
    }
}
darkmist002 darkmist002

2013/6/25

#
ok, updated the coding so that it would display a counter, but can't get it to update the number when a spawned unit get's eaten by the defenseSpot method. added code to the world:
LivesLeft life = new LivesLeft(d);
then changed the LivesLeft class:
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 hpBase;
    private defenseSpot d;
    public LivesLeft()
    {
        
    }
    
    public LivesLeft(defenseSpot def)
    {
        //healthLeft(health);
        //hpBase = health;
        //setImage(new GreenfootImage("" + hp, 20, Color.WHITE, Color.BLACK));
        d = def;
    }
    
    public void act() 
    {
        // Add your action code here.
        LifePic();
        
    }  
    
    private void LifePic()
    {
        setImage(new GreenfootImage("Lives Left: " + d.health(), 20, Color.WHITE, Color.BLACK));
    }
}
darkmist002 darkmist002

2013/6/25

#
ok, solved it. gonna leave it up here for anyone that has trouble like this. changed the references around, and changed the methods a little in both classes. world:
public tdpath()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        LivesLeft life = new LivesLeft();
        defenseSpot d = new defenseSpot(life);
        
        addObject(new spawning(), 35, 100);
        addObject(d, 100, 580);
        addObject(life, 170, 550);
        
    }
LivesLeft:
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 = 20;
    //private defenseSpot d;
    public LivesLeft()
    {
        
    }
    
    //public LivesLeft(defenseSpot def)
    //{
        //healthLeft(health);
        //hpBase = health;
        //setImage(new GreenfootImage("" + hp, 20, Color.WHITE, Color.BLACK));
        //d = def;
        //hp = d.health();
   // }
    
    public void act() 
    {
        // Add your action code here.
        checkHealth();
        LifePic();
        
    }  
    public void loseHealth()
    {
        hp--;
    }
    
    public int checkHealth()
    {
        //if(hp != d.health())
        //{
         //   hp = d.health();
        //}
        return hp;
    }
    
    private void LifePic()
    {
        setImage(new GreenfootImage("Lives Left: " + hp, 20, Color.WHITE, Color.BLACK));
    }
}
defenseSpot:
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.
     */
    private int totalHealth;
    private LivesLeft life;
    public defenseSpot(LivesLeft lf)
    {
        life = lf;
        totalHealth = life.checkHealth();
    }
    
    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);
            life.loseHealth();
            totalHealth = life.checkHealth();
            //totalHealth--;
           // health();
        }
        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;
    //}
}
You need to login to post a reply.