This site requires JavaScript, please enable it in your browser!
Greenfoot back
Sams0n
Sams0n wrote ...

2017/6/12

Progressive health bar

Sams0n Sams0n

2017/6/12

#
i want to create a health bar that slowly disappears piece by piece how do i do that
Super_Hippo Super_Hippo

2017/6/12

#
That is a method I used in one of my games to create a life bar (leben=current health, maxLeben=maximum health).
        public GreenfootImage createLifeBar(int leben, int maxLeben)
        {
            GreenfootImage img = new GreenfootImage(84, 24);
            img.setColor(Color.BLACK);
            img.fill();
            img.setColor(Color.GRAY);
            img.fillRect(2, 2, 80, 20);
            img.setColor(Color.GREEN);
            img.fillRect(2, 2, (int)(80*leben/maxLeben), 20);
            GreenfootImage num = new GreenfootImage(leben+"/"+maxLeben, 20, Color.BLACK, new Color(0,0,0,0));
            img.drawImage(num, img.getWidth()/2-num.getWidth()/2, 2);
            return img;
        }
You will have a life variable and keep decreasing it in the act method and call that method again. So something like that:
private int health, maxHealth;

public HealthBar(int max)
{
    maxHealth = max;
    health = maxHealth;
    setImage(createLifeBar(health, maxHealth));
}

public void act()
{
    setImage(createLifeBar(--health, maxHealth));
}
Maybe it is not exactly what you need, but I hope you get the idea.
Sams0n Sams0n

2017/6/13

#
thank you I have ben trying to do this for a few days
You need to login to post a reply.