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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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(); } } |