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

2018/1/23

health Bar propblem

yikes yikes

2018/1/23

#
I have spent several hours coding this game, and I watch a video to try to implement health bar.. and OMG it will not work. All it does in the game is take damage but the bar doesn't go down! after taking damage (I think it doesent update properly). So far I have this this code is used for my main character public void Health1() { Actor asteroid = getOneIntersectingObject(Asteroid.class); if (asteroid != null) { World myWorld = getWorld(); Space space = (Space)myWorld; HealthBar0 healthbar0 = space.getHealthBar0(); if(touchingAsteroid == false) { Greenfoot.playSound("Impact.wav"); touchingAsteroid = true; healthbar0.loseHealth0(); if (healthbar0.health <=0) { Greenfoot.playSound("ExplotionShip.wav"); getWorld().showText("You Died!",500,300); myWorld.removeObject(this); Greenfoot.stop(); } } } else { touchingAsteroid = false; } } This is the code for the Health (which is an actor) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HealthBar0 here. * * @author (your name) * @version (a version number or a date) */ public class HealthBar0 extends Actor { int health = 5; int healthBarWidth = 80; int healthBarHeight = 15; int pixelsPerHealthPoint = (int)healthBarWidth/health; /** * Act - do whatever the HealthBar wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public HealthBar0() { updateHealth(); } public void act() { updateHealth(); } public void updateHealth() { setImage (new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2)); GreenfootImage myImage = getImage(); myImage.setColor(greenfoot.Color.WHITE); myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1); myImage.setColor(greenfoot.Color.RED); myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight); } public void loseHealth0() { health--; } } this is what i have in my World Class import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Space here. * * @author (your name) * @version (a version number or a date) */ public class Space extends World { HealthBar0 healthbar0 = new HealthBar0(); /** * Constructor for objects of class Space. * */ public Space() { super(1000, 600, 1); addCharacters(); } public HealthBar0 getHealthBar0() { return healthbar0; } public void addCharacters() { addObject (new Ship(), 30, 300); addObject (new HealthBar0(), 50, 70); addObject (new Asteroid(), 400, 500); addObject (new Asteroid(), 800, 200); addObject (new Asteroid(), 600, 400); addObject (new Asteroid(), 500, 200); } public void act() { if (Greenfoot.isKeyDown("p")) { Greenfoot.setWorld( new PauseScreen()); } } } I am a noob at greenfoot so can you help me solve this problem ASAP.(and prob reply with my code but fixed)
CxVercility CxVercility

2018/1/23

#
Ehh... Please use the Code function next time (right under the message box) Firstly, your health bar shouldnt act on its own, just make the loseHealth() call update(). Secondly, your calculation of pixelsPerHealthPoint is (int)healthBarWidth/health while you fill your Rectangle with Health*pixelsPerHealthPoint That results in healthBarWidth/health*health which is always just healthBarWidth (and therefore static ignoring possible flooring through integer conversion)
yikes yikes

2018/1/24

#
Are you saying i should make loseHealth() and callupdate() separate methods?, I could be wrong but let me put my code back in the proper.Guys can you pls respond with my code fixed, i just started Greenfoot.
boolean touchingAsteroid = false;

public void Health1()
   { 
     Actor asteroid = getOneIntersectingObject(Asteroid.class);
        if (asteroid != null)
     {
        World myWorld = getWorld();
        Space space = (Space)myWorld;
        HealthBar0 healthbar0 = space.getHealthBar0();
            if(touchingAsteroid == false)
        {
          Greenfoot.playSound("Impact.wav");
          touchingAsteroid = true;
          healthbar0.loseHealth0();
        
            if (healthbar0.health <=0)
          {
                Greenfoot.playSound("ExplotionShip.wav");
                getWorld().showText("You Died!",500,300);
                myWorld.removeObject(this);
                Greenfoot.stop();
            
          }
        }
     } else {
        touchingAsteroid = false;
     }
    }





HealthBar0 healthbar0 = new HealthBar0();




public HealthBar0 getHealthBar0()
    {
        return healthbar0;
    }


addObject (new HealthBar0(), 50, 70);






import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HealthBar0 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HealthBar0 extends Actor
{
    int health = 5;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public HealthBar0()
    {
        updateHealth();
    }
    public void act() 
    {
         updateHealth();
    }  
    public void updateHealth()
    {
        setImage (new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(greenfoot.Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(greenfoot.Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
    }
    public void loseHealth0()
    { 
       health--;
    }  
}
danpost danpost

2018/1/24

#
The main prroblem is this:
// this line in your Space class
addObject (new HealthBar0(), 50, 70);
// should be this
addObject (healthbar0, 50, 70);
Otherwise, the one in the world is not the one that is updating.
yikes yikes

2018/1/24

#
THANK YOU SO mUCH, it WOrks now.
You need to login to post a reply.