Hi so i wanted to add a healthier to my game and i found some code on youtube i tried to use. it compiles ok. however, the healthcare doesn't update (change its picture) and when i right click it, and click the show health method, the health does not change( it stays at 20). BUT, after u hit the boss 20 times (Dealing 1 damage per hit) it triggers the command i have that checks if it has 0 health.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Healthbar3 here. * * @author (your name) * @version (a version number or a date) */ public class Healthbar3 extends HealthBars { int health = 20; int healthBarWidth = 80; int healthBarHeight = 15; int pixelsPerHealthPoint = healthBarWidth/health; public Healthbar3() { update(); } public void act() { update(); } public void update() { setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2)); GreenfootImage myImage = getImage(); myImage.setColor(Color.WHITE); myImage.drawRect(0, 0,healthBarWidth + 1, healthBarHeight + 1); myImage.setColor(Color.RED); myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight); } public int health() { return health; } public void loseHealth(int amount) { health = health - amount; update(); } }