hi there
im new to grenfoot and starting programming an online tutorial where you are a pie and are trying to hit banana; if they hit you, you loose health on your health bar; thats perfectly working, but now i want my pie to looses health ON ANOTHER HEALTHBAR after time and that you regain health after hitting another object.
and i have no idea how to to that; if someone of you experienced guys could help me?
thats my code of the healthbar:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class HealthBar100 extends Actor
{
int health = 100; // you have at the beginning 100 health points
int healthBarWidth = 102;
int healthBarHeight = 22;
int pixelsPerHealthPoint = (int)healthBarWidth/health;
public HealthBar100()
{
update();
}
public void act()
{
update();
}
public void update()
{
setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.BLACK);
myImage.drawRect(0, 0, healthBarWidth + 2, healthBarHeight + 2);
myImage.drawRect(1, 0, healthBarWidth, healthBarHeight); // makes the black frame thicker
myImage.drawRect(0, 1, healthBarWidth, healthBarHeight); // makes the black frame thicker
myImage.setColor(Color.RED);
myImage.fillRect(2, 2, health*pixelsPerHealthPoint, healthBarHeight - 2);
}
public void loseHealth25() // you lose 25 health points
{
health-=25;
}
and thats the code of the pie hitting a banana:
public void hitBanana() // if you touch a banana you lose health
{
Actor banana = getOneIntersectingObject(Banana.class);
if(banana !=null)
{
World world = getWorld();
Waterworld waterworld = (Waterworld)world;
HealthBar100 healthbar100 = waterworld.getHealthBar100();
if(touchingBanana== false)
{
healthbar100.loseHealth50();
touchingBanana = true;
if(healthbar100.health <=0)
// GameOver-Screen appears if healthbar reaches 0
{
GameOver gameover = new GameOver();
world.addObject(gameover, 300, 200);
world.removeObject(this);
}
Greenfoot.playSound("much.mp3");
}
} else {
touchingBanana = false;
}
}
as i said, these codes are working and the pie is loosing health, but i want it to loose health on ANOTHER healthbar automatically
thanks!!!

