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

2022/5/28

life bar

elvacan elvacan

2022/5/28

#
hello nobody knows how to make a life bar that is spent slowly. if anyone knows please send me the code
RcCookie RcCookie

2022/5/29

#
There is no general solution to a problem like this. But here is some useful information: If you want to do it the object oriented way, you may create a HealthBar class:
public class HealthBar extends Actor {
    private int health = 100;

    public void getHealth() { return health; }

    public void setHealth(int health) {
        this.health = health;
    }
}
To create a visual health bar, use GreenfootImage to set the objects image. Here are some important methods:
// Create an image with a specified width and height (the image is transparent by default)
GreenfootImage image = new GreenfootImage(102, 12);

// Set the color for subsequent operations
image.setColor(Color.RED);
// Use predefined constants or custom rgb values
image.setColor(new Color(50, 100, 150);

// Draws the outline of a rectangle with top left at coordinates 0/0 and a size of 102x12 pixels
image.drawRect(0, 0, 101, 11);

// Fills a rectangle with the top left at coordinates 1/1 and a size of 100x10 pixels
image.fillRect(1, 1, 99, 9);

// Set the image of the current actor to the specified image
setImage(image);
For the actual health loosing logic, I can give you little advice; it very much depends on your usecase. The general idea is that you decrement the value in your health bar when you do something that looses you health, and possibly increment it from time to time to have regeneration.
You need to login to post a reply.