I am making a game in which there is a health bar for actor. Basically what I'm doing is creating an image, setting the X value to the health, and when the health gets lowered, the image gets smaller. After that I move the image to the left so it covers the space on the left side. It was working fine until a few hours ago, when I made some other changes to my game, (which is pretty big), and it gave an error. I have already tried not making a new image every time but that still doesn't work.
Here is the code for the health bar:
Thanks for help in advance!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class Healthbar extends Tank
{
double RealHealth = 0, MaxHealth = 0;
int perc = 0;
double small;
Tank theTank;
boolean once = true;
int currentX = 0;
public Healthbar(Tank partheTank){
super("",200);
//where = parWhere;
theTank = partheTank;
}
public void act()
{
if(once == true){
MaxHealth = theTank.maxHealth;
RealHealth = theTank.realHealth;
getWorld().addObject(new Outline(0), getX(), getY());
currentX = getX();
once = false;
}
setStuff();
checkDead();
if(!dead){
setImage();
}
}
public void checkDead(){
if(perc <= 1){
theTank.dies();
}
}
public void setStuff(){
MaxHealth = (double)theTank.maxHealth;
RealHealth = (double)theTank.realHealth;
small = RealHealth/MaxHealth;
perc = (int) (small * 200);
int num = 200-perc;
setLocation((currentX)-num/2, getY());
}
public void setImage(){
GreenfootImage image = new GreenfootImage(perc, 15);//This is the line that brings the error
if(perc >= 100){
image.setColor(java.awt.Color.GREEN);
}
else if(perc < 100&& perc > 70){
image.setColor(java.awt.Color.YELLOW);
}
else if(perc <= 70 && perc > 50 ){
Color color = new Color(255,60,30);
image.setColor(color);
}
else if(perc <= 50){
image.setColor(java.awt.Color.RED);
}
image.fill();
setImage(image);
}
}

