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

2018/12/7

Health Bar issues

pham pham

2018/12/7

#
I need help on getting my Health Bar to work, I thought everything was going fine. I have this one error that I don't know how to fix. I'm also doing a multiplayer game, so I'm not sure if that affects anything.
import greenfoot.*;  

public class MyWorld extends World
{
    HealthBar1 healthbar1 = new HealthBar1();
    HealthBar2 healthbar2 = new HealthBar2();
    Player1 player1 = new Player1();
    Player2 player2 = new Player2();

    public MyWorld()
    {    
        super(600, 400, 1); 
        
        prepare();
    }
    
    private void prepare()
    {
        addObject(healthbar1, 100, 40);
        addObject(healthbar2, 500, 40); 
        addObject(player1, 600, 250);
        addObject(player2, 100, 250);
    }
    
    public HealthBar1.getHealthBar1()
    {
        return healthbar1;
    }
    
    public HealthBar2.getHealthBar2()
    {
        return healthbar2;
    }

}
It says there's an error with the last two codes, "public HealthBar1..." The rest of my code is fine, but when I try to delete this, then errors show up in my players code.
import greenfoot.*; 

public class Player1 extends Players
{
    boolean bulletshot = false;
    private int bulletTimer = 0;
    
    public void act() 
    {
        setRotation(90);
        if (Greenfoot.isKeyDown("W"))
        { move(-4); }
        if (Greenfoot.isKeyDown("S"))
        { move(4); }
         setRotation(0);

        shoot();
        shot();
    } 
    
    public void shoot()
{
    if (bulletTimer < 50) 
    {
        bulletTimer++;
        return;
    }
     
    if (Greenfoot.isKeyDown("D")) 
    {
        Bullets bullets = new Bullets(); 
        getWorld().addObject(bullets, getX(), getY());
        bullets.setRotation(getRotation());
        bullets.move(5);
        bulletTimer = 0;
    }
}

    public void shot()
    {
        Actor Bullet = getOneIntersectingObject (Bullets.class);
        if (Bullet != null)
        {
            World myWorld = getWorld();
            HealthBar1 healthbar = MyWorld.getHealthBar1();
            if  (bulletshot  ==  false)
            { 
                healthbar.loseHealth();
                bulletshot =  true;
                if (healthbar.health <=0)
                {
                    Win2 win2 = new Win2();
                    MyWorld.addObject(win2, MyWorld.getWidth()/2, MyWorld.getHeight()/2);
                    MyWorld.removeObject(this);
                }

        }
    } else {
        bulletshot= false;
    }
}
}
import greenfoot.*; 
import greenfoot.Color;

public class HealthBar1 extends Player1
{
   int health = 4;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int healthPoint = healthBarWidth/health;
    
    public void act() 
    {
        update();
    }    
    
    public HealthBar1()
    {
        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.GREEN);
        myImage.fillRect(1, 1, health*healthPoint, healthBarHeight);
    } 
    
    public void loseHealth()
    {
        health--;
        update();
    }
}
I need help with this issue. Thanks.
danpost danpost

2018/12/7

#
pham wrote...
I have this one error that I don't know how to fix. << Code Omitted >> It says there's an error with the last two codes, "public HealthBar1..."
In your MyWorld class, there should be no dots ('.') in lines 25 and 30. Change them to spaces (' ') to separate the return type from the method name. You should not need two different health bar classes (nor is it necessary to have two separate player classes). Also a health bar is not a player -- it is a different type of actor. The HealthBar class should extend Actor.
pham pham

2018/12/7

#
Thank you!
You need to login to post a reply.