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

2015/3/16

Healthbar doesn't decrease

SalatgurkenJoe SalatgurkenJoe

2015/3/16

#
I have a problem with my little game, if i touch an enemy i get damaged and the healthbar should decrease but it doesn't. Altough the damaging works, the healthbar doesn't decrease. Here is my code:
public class HealthBar extends Erm
{
    int health = 5;
    int healthBarWidth =80;
    int healthBarHeight =15;
    int pixelsPerHealthPoint = (int) healthBarWidth/health;
    
    
    
    public HealthBar () //constructer,runs automatically, creates the healthbar even if the game isnt running
    {
        update ();
    }
    
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    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 void loseHealth ()
    {
        health --;
    }
}
danpost danpost

2015/3/16

#
Usually, when this issue comes up, the problem ends up to be in the World subclass. Please post its code.
SalatgurkenJoe SalatgurkenJoe

2015/3/16

#
Thank you for answering so quickly!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class World1 here. * * @author (your name) * @version (a version number or a date) */ public class ScrollingWorld extends World { GreenfootSound myMusic = new GreenfootSound ("BGMelodie.mp3"); public Player thePlayer; public Erm theErm; HealthBar healthbar = new HealthBar (); Counter counter = new Counter (); public ScrollingWorld() { super(1000, 600, 1, false); //The extra 'false' parameter means that the world is unbounded. addTiles(); thePlayer = new Player(); addObject(thePlayer, 150, 420); //Creates the player. theErm = new Erm(); addObject(theErm, 0, -10); prepare(); } /** * Creates tiles for building your world. */ public void addTiles() { for(int i=1;i<2;i++) { addObject(new Block14h(), i*30+180, 500); addObject(new Block6h(), i*30+570, 500); addObject(new Block2h(), i*30+750, 450); addObject(new Block4h(), i*30+900, 450); addObject(new Block4h(), i*30+1080, 450); addObject(new Block8h(), i*30+1290, 350); addObject(new Block10v(),i*30-15, 335); } } public Counter getCounter () { return counter; } /** * Creates enemies. * */ public Player getPlayer() { return thePlayer; } public Erm getErm() { return theErm; } public HealthBar getHealthBar () //makes the player to be able to get the method { return healthbar; //Player can now call this method } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { for (int o=1;o<2;o++) { Platform platform = new Platform(); addObject(platform, 442, 399); Platform platform2 = new Platform(); addObject(platform2, 922, 301); Sandwich sandwich = new Sandwich(); addObject(sandwich, 563, 337); addObject(counter, 54, 30); HealthBar healthbar = new HealthBar(); addObject (healthbar, 170, 30); removeObject(platform); removeObject(platform2); sandwich.setLocation(837, 291); removeObject(sandwich); addObject (healthbar, 200, 40); Enemy1 enemy1 = new Enemy1(); addObject(enemy1, 326, 438); Enemy1 enemy12 = new Enemy1(); addObject(enemy12, o+1290, 292); } Sandwich sandwich2 = new Sandwich(); addObject(sandwich2, 256, 437); Music music = new Music(); addObject(music, 937, 28); } }
danpost danpost

2015/3/16

#
In the middle of your 'prepare' method you are creating a separate healthbar and not assigning it to your 'healthbar' field; you are assigning it to a local variable. Remove one of the instances of "new HealthBar()" (or, rather, just remove that line).
SalatgurkenJoe SalatgurkenJoe

2015/3/17

#
ahh, thank you very much. now it works.
You need to login to post a reply.