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

2018/6/9

Boss Health Bar not Updating

Nines Nines

2018/6/9

#
I'm having an issue with my boss' health. This is for a class assignment and I need the value to change as the player's attack hits the boss. Currently the bossHealth value only changes in one class and not the other. This is an issue because I draw boss health bar in a separate class but directed greenfoot to look in the specific class that contains the health value to update it. *Directed bullet to boss class to check if it is intersecting *Then Directed to boss constructur in boss class *Lastly directed to bossHp(boss health bar) for the boss health value I tried to debug it and it shows that once the bullet hits the boss the "bossHit.bossLife.bossHealth" decreases(printed it out in the terminal). However when I go to the bossHealth variable in the BossHp class and print that one out it remains constant. So it never matches the updated value in the bullet class(where health for the boss is being subtracted once it hits the boss). I've concluded that it is an issue of greenfoot not being able to find the correct value and update it when it is drawn in bossHP.class. I've added the boss health bar into the world as well so I do see it just that it doesn't decrease as bullets hit the boss. Any help with what the issue is will be very much appreciated. BOSS CLASS:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class Boss here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boss extends Actor
{
    public int rotation;
    public int rotationDelay = 150;
    private boolean isRotate = false;
    int bossStep = 0;
    int bossBulletDelay = 0;
    public List<BossBullet> bossBullets;
    private Actor player;
    public BossHp bossLife;
    public Boss()
    {
        bossBullets = new ArrayList<BossBullet>();
        bossLife = new BossHp();
BULLET CLASS(The one that hits the boss)
       public void bossBeenHit()
    {
        bossLoseHealth = 20;
        Boss bossHit = (Boss)getOneIntersectingObject(Boss.class);
        if (bossHit != null)
        {
            System.out.println("BossHit");
            bossHit.bossLife.bossHealth -= bossLoseHealth;
            getWorld().removeObject(this);            
            System.out.println("BulletRemoved");
            if (bossHit.bossLife.bossHealth < 0)
            {
                bossHit.bossLife.bossHealth = 0;
            }
            System.out.println(bossHit.bossLife.bossHealth);            
        }       
    }
}
BOSSHP CLASS (This contains the health value)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BossHp here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BossHp extends Actor
{   
    int healthWidth = 100;
    int healthHeight = 15;
    int danger = 200;
    public int bossHealth = 300;
    /**
     * 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() 
    {
        // Add your action code here.
        updateBossHp();                
    }
     public void updateBossHp()
    {
        GreenfootImage myImage = new GreenfootImage( bossHealth + 2, healthHeight + 2);
        System.out.println("updateBossHp(): " + bossHealth);
        myImage.drawRect(0, 0, healthWidth+ 1, healthHeight + 1);
        setImage(myImage);        
        if (bossHealth > healthWidth/2)
        {
            myImage.setColor(Color.GREEN);
            myImage.fillRect(1, 1, bossHealth *2, healthHeight);
        }
        else if (bossHealth <= healthWidth/2)
        {
            myImage.setColor(Color.YELLOW);
            myImage.fillRect(1, 1, bossHealth *2, healthHeight);
           if(bossHealth < danger)
           {
            myImage.setColor(Color.RED);
            myImage.fillRect(1, 1, bossHealth *2, healthHeight);
           }        
        }
    }
}
danpost danpost

2018/6/9

#
Nines wrote...
I need the value to change as the player's attack hits the boss. Currently the bossHealth value only changes in one class and not the other. This is an issue because I draw boss health bar in a separate class but directed greenfoot to look in the specific class that contains the health value to update it. ... I do see it just that it doesn't decrease as bullets hit the boss. << Code Omitted >>
You see it, but did you actually check its value. After you believe it should have decreased, do the following: * pause the scenario * right-click on BossHp object * select Inspect from the pop-up menu * find the int bossHealth field * report back the value you find it to be
You need to login to post a reply.