This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
Ruv25808
Ruv25808 wrote ...

2017/2/16

Health bar troubles

Ruv25808 Ruv25808

2017/2/16

#
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.
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();
    }
}
danpost danpost

2017/2/16

#
You need to also show all codes in other classes that deal with Healthbar3 objects (anywhere you declare variables of that type or hold a healthbar; where you add any into a world; where you adjust its value; pretty much everywhere you deal with one). Indicate where each piece of code is located. At least start with everything in your World subclass.
Ruv25808 Ruv25808

2017/2/16

#
right This is the boss who has the health bar
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Boss1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss1 extends Enemies
{
    boolean touchingShot = false;
    boolean touchingFlash = false;
    private int gunReloadTime;
    private int reloadDelayCount;
    public Boss1()
    {
        gunReloadTime = 10;
        reloadDelayCount = 0;
    }

    /**
     * Act - do whatever the Boss1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        hurting();
        Shoot();
        reloadDelayCount++;
    }

    public void Shoot()
    {
        if(Greenfoot.getRandomNumber(1000) < 2) 
        {
            fire();
        }
    }

    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }

    public void hurting()
    {

        Actor shot = getOneIntersectingObject(Shot.class);
        if(shot!= null)
        {

            BossWorld bossworld = (BossWorld)getWorld();
            Healthbar3 healthbar = bossworld.gethealthbar();            
            getWorld().removeObject(shot);
            if(touchingShot == false)
            {
                healthbar.loseHealth(1);
                healthbar.update();
                touchingShot=true;
                if(healthbar.health <= 0)
                {
                    getWorld().removeObject(this);
                }
            }
        }
        else
        {
            touchingShot = false;
        }       
    }
Ruv25808 Ruv25808

2017/2/16

#
This is the code for the world class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BossWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BossWorld extends World
{
    Healthbar3 healthbar = new Healthbar3();
    /**
     * Constructor for objects of class BossWorld.
     * 
     */
    public BossWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 600, 1);
        prepare();
    }
    
    public Healthbar3 gethealthbar()
    {
        return healthbar;
    }
    
    public void act()
    {
        setPaintOrder(Game_Over.class, SuperAsteroid.class, Asteroid.class, Nyancat.class,
        Boss1.class, Shot.class);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Boss1 boss1 = new Boss1();
        addObject(boss1,300,200);
        Nyancat nyancat = new Nyancat();
        addObject(nyancat, 300, 500);
        Healthbar3 healthbar = new Healthbar3();
        addObject(healthbar, 300, 110);
    }
}
Ruv25808 Ruv25808

2017/2/16

#
thx in advance :D
danpost danpost

2017/2/16

#
Remove the first word, 'Healthbar3' from line 44 in the BossWorld class.
Super_Hippo Super_Hippo

2017/2/16

#
You can even remove the whole line.
Ruv25808 Ruv25808

2017/2/17

#
That didn't do anything
Ruv25808 Ruv25808

2017/2/17

#
is there anything i need to change in the healthbar3 class
Super_Hippo Super_Hippo

2017/2/17

#
It is obviously not needed to call the update method like 100 times each act cycle... I would suggest to pause the scenario after the bullet hit the Boss and check the health field in the healthbar.
danpost danpost

2017/2/17

#
Ruv25808 wrote...
is there anything i need to change in the healthbar3 class
The healthbar3 class is fine the way it is -- do not modify it. I tested out the classes given above and after removing line 44 of the BossWorld class, the healthbar performed appropriately when a Shot object "hit" the Boss1 object. If yours is still not working, I would suggest you post up the HealthBars class next for inspection.
You need to login to post a reply.