I am having the hardest time to subtract hp form the bar when a player is hit. Any code help would be amazing!
private void showLive() {
GreenfootImage image = new GreenfootImage(getImage());
if (ein==false) {
sleben=(int)Leben; //sleben is the life at the game start and Leben is a double
ein=true;
}
image.setColor(Color.BLACK);
image.drawRect(0, 25, 25, 3); //"box" for the bar in black
image.setColor(Color.RED);
image.fillRect(1, 26, 24, 2); //fill the box with red
image.setColor(Color.GREEN);
minusHealth=24.0/sleben*(int)Leben; //thats the important part, my bar is 24 pixels long so you do 24 / the maxvalue * the current value
image.fillRect(1, 26, (int)minusHealth, 2);
setImage(image);
}// instance field
Bar healthBar;
// created in constructor or while instance field is declared
healthBar = new Bar("Health", "", 100, 100); // similar
// then code to adjust value would be
healthBar.add(-damageAmount);// in your world class constructor (or method it calls)
Bar bar = new Bar("Health", "", 100, 100);
addObject(bar, /* wherever */);
Player player = new Player(bar);
// instance field in Player class
Bar healthbar;
// Player class constructor
public Player(Bar bar)
{
healthbar = bar;
/* any other code */
}
// in act method or method it calls when damage taken
healthbar.add(-damageAmount);