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

2019/11/27

Health Bar isn't working

annika.g annika.g

2019/11/27

#
For my remake of Pacman I thought about adding a health bar to the game. I followed a youtube tutorial. My Greenfoot programme is saying there are no errors but it's not working. The health bar isn't going down when Pacman touches the BlauerGeist (blue ghost) This is the code for the Pacman:
public void hitBlauerGeist()

        {
            Actor BlauerGeist = getOneIntersectingObject(BlauerGeist.class);
            if (BlauerGeist!=null)
            {
                World myWorld = getWorld();
                RoboterWelt roboterwelt =  (RoboterWelt)myWorld;
                HealthBar healthbar = roboterwelt.getHealthBar();
                if (touchingBlauerGeist == true)
                {
                    healthbar.loseHealth();
                    touchingBlauerGeist = true;
                    
                    if(healthbar.health <=0)
                    {
                        
                        myWorld.removeObject(this);
                    }
                }
               
            
        }
        else 
        { touchingBlauerGeist = false;
           
        }
This is the code for the class HealthBar:
public class HealthBar extends Actor
{
    int health = 3;
    int healthBarWidth = 100;
    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 HealthBar()
    {
        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 void loseHealth()
    {
        health --;
    }
}
In the world class I defined the health bar and put in this code:
public HealthBar getHealthBar()
    {
        return healthbar;
    }
    
    private void prepare()
    {   
        addObject(healthbar, 21, 32);
}
I hope somebody can help me with this problem, thank you.
danpost danpost

2019/11/27

#
Please show ALL codes in your RoboterWelt class dealing with the health bar.
annika.g annika.g

2019/11/29

#
This is the whole code of the world to the point of prepare. Everything after the coordinates of the health bar is just how my world looks.

public class RoboterWelt extends World
{  
    
    private static int zellenGroesse = 20;
    private int score = 0;
    HealthBar healthbar = new HealthBar();


    /**
     * Erschaffe eine Welt mit 26 * 34 Zellen.
     */
    
    public RoboterWelt()
    {
        super(26, 34, zellenGroesse);
        GreenfootImage bg = new GreenfootImage ("Black_from_a_camera.jpg");
       
        
        setPaintOrder(Schraube.class, Akku.class,  Roboter.class, Wand.class);
        Greenfoot.setSpeed(35);   
        prepare();
    }
    public HealthBar getHealthBar()
    {
        return healthbar;
    }
    
    private void prepare()
    {   
        addObject(healthbar, 21, 32);
nccb nccb

2019/11/29

#
Your touchingBlauerGeist variable doesn't look like it can be set to true. You check if it's true already before you set it to true. I think if you remove the line "if (touchingBlauerGeist == true)" and keep the body of that if, it may work.
annika.g annika.g

2019/11/29

#
Wow, thank you so much!! You solved my problem, it's working now!! :)
annika.g annika.g

2019/11/29

#
Now I have another problem though; Pacman is removed from the world after he was hit 2 times by the ghosts but I want him to be removed when the ghosts hit him 3 times, and the heath bar is empty. How do I change that?
danpost danpost

2019/11/29

#
I think your original line 10 in the hitBlauerGeist method was supposed to check for false -- not true.
annika.g annika.g

4 days ago

#
Thank you, its working! :)
You need to login to post a reply.