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

2016/5/31

Health

Walker Walker

2016/5/31

#
I have a basic health code but i need help with a counter and if it touches an actor to decrease
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class HealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 * 
 */
public class health extends Soul    
{
    boolean inPosition = false;    
    int health;
    int width=80;
    int height=15;
    int healthPerPixel;
    public health(int hp)
    {        
        health=hp;
        
        healthPerPixel=(int)(width/health); update();
        
    }
     
    public void act() 
    {
        
        
    }
         
    public void update()
    {
        setImage(new GreenfootImage(width+2,height+2));
        GreenfootImage h=getImage();
        h.setColor(Color.BLACK);
        h.drawRect(0,0,width+1,height+1);
        h.setColor(Color.GREEN);
        h.fillRect(1,1,health*healthPerPixel,height);
    }
     
    public void loseHealth()
    {  
        //health=health-tower.dmg;
        update();
    }
     

}
Super_Hippo Super_Hippo

2016/5/31

#
The class name should start with an uppercase letter. So change 'health' to 'Health' although 'Healthbar' would be better.
if (isTouching(ClassName.class))
{
    ((Health) (getWorld().getObjects(Health.class).get(0))).loseHealth();
}
In the loseHealth-method, you could add the following line (before the call to the update-method) to decrease the health:
health--;
You need to login to post a reply.