I am making a game rigt now with a character that is able to loose hp. I want to show the hp with a healthbar by replacing the image of my healthbar everytime the actor gets damage, but i dont know how to do it.
private void updateHealthBarImage()
{
GreenfootImage img = ...
...
setImage(img);
}hp--; updateHealthBarImage();
private void updateHealthBarImage()
{
GreenfootImage img = ...
...
setImage(img);
}hp--; updateHealthBarImage();
public void leben(int amount)
{
hp = amount;
if(unverwundbarkeit == false)
{
if(isTouching(kleinespinne.class))
{
getWorld().removeObjects(getObjectsInRange(80,kleinespinne.class));
unverwundbarkeit = true;
hp-=1;
}
if(isTouching(Kristall.class))
{
getWorld().removeObjects(getObjectsInRange(128,Kristall.class));
unverwundbarkeit = true;
hp -= 1;
}
if(hp<1)
{
Greenfoot.setWorld(new verloren());
}
}
if (unverwundbarkeit == true)
{ this.timer3 = this.timer +1;
if(this.timer3 > 400)
{
this.timer3 = 0;
unverwundbarkeit = false;
}
}
}public void act()
{
if(! getWorld().getObjects(Jim.class).isEmpty())
{
Jim jim = (Jim)getWorld().getObjects(Jim.class).get(0);
int jimX = jim.getX();
int jimY = jim.getY();
setLocation(jimX,jimY -50);
}
}
public void jimhpupdate(){
MyWorld MyMyWorld = (MyWorld) getWorld();
Jim jim = MyMyWorld.getjim();
jim.leben(5);
if(hp==5)
{
setImage("jimhp1.png");
}
if( hp==4)
{
setImage("jimhp2.png");
}
if(hp==3)
{
setImage("jimhp3.png");
}
if(hp==2)
{
setImage("jimhp2.png");
}
if(hp==1)
{
setImage("jimhp1.png");
}
}
public Jim getjim()
{ return jimm;
}
this.timer3 = this.timer +1;