I want my HealthBar class to follow my Greep class once they are both created in the world, and to disappear once the greep reaches zero health.
Currently, the health bar will be created in the world, but will not follow my Greep class.
HealthBar class:
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 HealthBar extends Actor
{
boolean inPosition = false;
int health;
int width=80;
int height=15;
int healthPerPixel;
public HealthBar(int hp)
{
health=hp;
healthPerPixel=(int)(width/health);
update();
}
public void act()
{
}
public void update()
{
setImage(new GreenfootImage(width+2,height+2));
GreenfootImage hb=getImage();
hb.setColor(Color.BLACK);
hb.drawRect(0,0,width+1,height+1);
hb.setColor(Color.GREEN);
hb.fillRect(1,1,health*healthPerPixel,height);
}
public void loseHealth()
{
//health=health-tower.dmg;
update();
}
public void followGreep()
{
if (Greeps.class!=null)
{
int x=Greeps.getX();
int y=Greeps.getY();
turnTowards(x,y);
move(1);
}else{ getWorld().removeObject(this);}
}
}
