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

2017/3/25

HealthBar creation

1
2
3
danpost danpost

2017/3/25

#
ironphoenix20 wrote...
yes, i get that but how do i actually test if the bar changes color? and i still dont understand what the first two parameters of the fillRect() method are
They are the x and y coordinate on the image being drawn on where the top left corner is (0, 0) and increasing going right and down.
ironphoenix20 ironphoenix20

2017/3/25

#
Here's my new code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * HealthBar generates and maintains a custom-sized image of a Health Bar
 * in green and red (red being missing HP).
 * 
 * Accepts any size - use Constants.
 * 
 * NOT FINISHED! Forgive my lack of commenting...
 * 
 * @author Jordan Cohen
 * @version 0.0.1 (2012)
 */
public class HealthBar extends Actor
{
    private double hp = 100;
    private double currHp = 100;
    private double damage = 5;
    private double xCoor;
    private double yCoor;
    
    // Declare Instance Variables
    
    private double currPercentHP;
    private int redBarSize;
    private int greenBarSize;

    // Declare Instance Images
    private GreenfootImage bar;
    private GreenfootImage greyBar;
    
    // Some constants - can be changed to suit size of related objects
    private final int HP_BAR_WIDTH = 150;
    private final int HP_BAR_HEIGHT = 20;
    private final int OFFSET = 36;

    // Declare Instance Objects
    private Actor target;

    // Declare some Color objects
    private Color myGreen = new Color (0, 255, 0);
    private Color myRed = new Color (255, 0, 0);
    private Color myGrey = new Color (169, 169, 169);

    /**
     * Main constructor - this is only called by the other Constructors
     * and is not intended to be called directly.
     */
    public HealthBar()
    {
        bar = new GreenfootImage(HP_BAR_WIDTH, HP_BAR_HEIGHT);
        greyBar = new GreenfootImage(100,100);
        bar.setColor(myGreen);
        bar.fill();
        //bar.setColor(myGrey);
        //bar.fill();
        //greyBar.setColor(myGrey);
        //greyBar.fill();
        /*if (currHP == maxHP)
        {*/
            this.setImage(bar);
            //this.setImage(greyBar);
        }
        /*else
        {
            this.setImage(bar);
        }*/
    

    /**
     * Constructor that takes one int - for objects starting with max hit points.
     * This will set both current and maximum hit points to the same value.
     */
    public HealthBar(int inMaxHP)
    {
        this(); // Calst he Main constructor (above)
        hp = inMaxHP;
        currHp = inMaxHP;
    }

    /**
     * Constructor takes an int for current and max hitpoints and also takes in an
     * Actor, which this HP Bar will follow - Whenever the Actor moves, so will this
     * hp bar. If the Actor is removed from the World, this HP bar will destroy itself.
     * 
     * NOTE: This is the Constructor used in the Bug simulation
     */
    public HealthBar (int inMaxHP, Actor target)
    {
        this(inMaxHP);
        this.target = target;
    }

    /**
     * Constructor that takes in a different value for current and max HP, ideal for
     * when a new health bar is needed for an Actor that doesn't have full HP.
     */
    public HealthBar(int inMaxHP, int inCurrHP)
    {
        this();
        hp = inMaxHP;
        currHp = inCurrHP;
    }

    /**
     * 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() 
    {
        /*if (target.getWorld() != null)
        {
            setLocation (target.getX(), target.getY() - OFFSET);
        }
        else
            getWorld().removeObject(this);*/
        setLocation(200,200);
    }    

    /**
     * update Method:
     * 
     * Expects new current HP
     * 
     * Returns true if HP has changed (needs an update)
     * Returns false if HP has not changed (to avoid excessive processing)
     */
    public void update (int newCurrHP)
    {
        for(int i=0; i<5; i++)
        {
            currPercentHP = (double) currHp / hp;
            greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
            bar.setColor(myGreen);
            bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
            bar.setColor(myGrey);
            bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
            //greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
            this.setImage(bar);
        }
        //return true;
        
        
        /*// Don't do anything if current HP hasn't changed
        if (newCurrHP != currHP)
        {
            if (newCurrHP == maxHP)
            {
                this.setImage(blank);
                //return false;
            }
            else
            {
                
                // Redraw HP bar w/ appropriate amount of green and red based on
                // current HP divided by max HP
                currHP = newCurrHP;
                currPercentHP = (double) currHP / maxHP;
                greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
                redBarSize = HP_BAR_WIDTH - greenBarSize;
                //Troubleshooting code:
                //System.out.println("CurrHP: " + currHP + " curr%HP: " + currPercentHP);
                //System.out.println("GreenBar: " + greenBarSize + " RedBar: " + redBarSize);
                bar.setColor(myGreen);
                bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
                bar.setColor(myRed);
                bar.fillRect(greenBarSize, 0, redBarSize, HP_BAR_HEIGHT);
                this.setImage(bar);
                //return true;
            }
        }
        //return false;*/
    }
}
ironphoenix20 ironphoenix20

2017/3/25

#
but nothing changes if i change the (0,0,etc, etc) to a different value. why is this?
danpost danpost

2017/3/25

#
ironphoenix20 wrote...
how do i actually test if the bar changes color?
Manually create a Healthbar object and then manually call the 'update' method on it, giving it a new value (try 75, 50, 25 and 0 -- then do 100 again).
danpost danpost

2017/3/25

#
I just noticed a couple of things. First, you are not changing the value of 'currHp' in the update method; the other is why is there a loop at 130?
ironphoenix20 ironphoenix20

2017/3/25

#
sorry about the loop. i was just trying something out there. but let me try your suggestion and get back to you on that.
ironphoenix20 ironphoenix20

2017/3/26

#
heres my current code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * HealthBar generates and maintains a custom-sized image of a Health Bar
 * in green and red (red being missing HP).
 * 
 * Accepts any size - use Constants.
 * 
 * NOT FINISHED! Forgive my lack of commenting...
 * 
 * @author Jordan Cohen
 * @version 0.0.1 (2012)
 */
public class HealthBar extends Actor
{
    private double hp = 100;
    private int currHp = 100;
    private double damage = 5;
    private double xCoor;
    private double yCoor;
    
    // Declare Instance Variables
    
    private double currPercentHP;
    private int redBarSize;
    private int greenBarSize;

    // Declare Instance Images
    private GreenfootImage bar;
    private GreenfootImage greyBar;
    
    // Some constants - can be changed to suit size of related objects
    private final int HP_BAR_WIDTH = 150;
    private final int HP_BAR_HEIGHT = 20;
    private final int OFFSET = 36;

    // Declare Instance Objects
    private Actor target;

    // Declare some Color objects
    private int red=0;
    private int green=255;
    private Color myGreen = new Color (red, green, 0);
    private Color myRed = new Color (255, 0, 0);
    private Color myGrey = new Color (169, 169, 169);

    /**
     * Main constructor - this is only called by the other Constructors
     * and is not intended to be called directly.
     */
    public HealthBar()
    {
        bar = new GreenfootImage(HP_BAR_WIDTH, HP_BAR_HEIGHT);
        greyBar = new GreenfootImage(100,100);
        bar.setColor(myGreen);
        bar.fill();
        //bar.setColor(myGrey);
        //bar.fill();
        //greyBar.setColor(myGrey);
        //greyBar.fill();
        /*if (currHP == maxHP)
        {*/
            this.setImage(bar);
            //this.setImage(greyBar);
        }
        /*else
        {
            this.setImage(bar);
        }*/
    

    /**
     * Constructor that takes one int - for objects starting with max hit points.
     * This will set both current and maximum hit points to the same value.
     */
    public HealthBar(int inMaxHP)
    {
        this(); // Calst he Main constructor (above)
        hp = inMaxHP;
        currHp = inMaxHP;
    }

    /**
     * Constructor takes an int for current and max hitpoints and also takes in an
     * Actor, which this HP Bar will follow - Whenever the Actor moves, so will this
     * hp bar. If the Actor is removed from the World, this HP bar will destroy itself.
     * 
     * NOTE: This is the Constructor used in the Bug simulation
     */
    public HealthBar (int inMaxHP, Actor target)
    {
        this(inMaxHP);
        this.target = target;
    }

    /**
     * Constructor that takes in a different value for current and max HP, ideal for
     * when a new health bar is needed for an Actor that doesn't have full HP.
     */
    public HealthBar(int inMaxHP, int inCurrHP)
    {
        this();
        hp = inMaxHP;
        currHp = inCurrHP;
    }

    /**
     * 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() 
    {
        /*if (target.getWorld() != null)
        {
            setLocation (target.getX(), target.getY() - OFFSET);
        }
        else
            getWorld().removeObject(this);*/
        setLocation(200,200);
        update();
    }    

    /**
     * update Method:
     * 
     * Expects new current HP
     * 
     * Returns true if HP has changed (needs an update)
     * Returns false if HP has not changed (to avoid excessive processing)
     */
    public void update (/*int newCurrHP*/)
    {
       if (hit()){
           red+=damage;
           green-=damage;
           greenBarSize-=damage;
           //barLength+=damage;
           //remake the bars
           myGreen= new Color(red, green, 0);
           //currPercentHP = (double) newCurrHP / hp;
           //greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
           bar.setColor(myGreen);
           bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
           bar.setColor(myGrey);
           bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
           //greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
           this.setImage(bar);
        }
           //return true;
        
        
        /*// Don't do anything if current HP hasn't changed
        if (newCurrHP != currHP)
        {
            if (newCurrHP == maxHP)
            {
                this.setImage(blank);
                //return false;
            }
            else
            {
                
                // Redraw HP bar w/ appropriate amount of green and red based on
                // current HP divided by max HP
                currHP = newCurrHP;
                currPercentHP = (double) currHP / maxHP;
                greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
                redBarSize = HP_BAR_WIDTH - greenBarSize;
                //Troubleshooting code:
                //System.out.println("CurrHP: " + currHP + " curr%HP: " + currPercentHP);
                //System.out.println("GreenBar: " + greenBarSize + " RedBar: " + redBarSize);
                bar.setColor(myGreen);
                bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
                bar.setColor(myRed);
                bar.fillRect(greenBarSize, 0, redBarSize, HP_BAR_HEIGHT);
                this.setImage(bar);
                //return true;
            }
        }
        //return false;*/
    }
    
    public boolean hit()
    {//checks to see if hit by dangerous item
        return true;
    }
}
ironphoenix20 ironphoenix20

2017/3/26

#
for some reason, greenfoot says on line 139 that the color parameters are outside the expected range. why is it saying this?
ironphoenix20 ironphoenix20

2017/3/26

#
heres the entire error: java.lang.IllegalArgumentException: Color parameter outside of expected range: Red Green at java.awt.Color.testColorValueRange(Color.java:310) at java.awt.Color.<init>(Color.java:395) at java.awt.Color.<init>(Color.java:369) at greenfoot.Color.<init>(Color.java:120) at HealthBar.update(HealthBar.java:139) at HealthBar.act(HealthBar.java:120) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
ironphoenix20 ironphoenix20

2017/3/26

#
also, my bar is currently starting as a grey color completely. can you see why this is? it should be entirely green at the beginning.
ironphoenix20 ironphoenix20

2017/3/26

#
ok, my bar starts out as green now but immediately turns grey when i run it (and not gradually decreasing the green part of the code while changing the green color to more reddish as it should be). what am i doing wrong? sorry, i know im asking a lot of questions but i could really use the help. thanks a lot.
ironphoenix20 ironphoenix20

2017/3/26

#
here is my current update method:
public void update (/*int newCurrHP*/)
    {
       if (hit()){
           red+=damage;
           green-=damage;
           greenBarSize-=damage;
           //barLength+=damage;
           //remake the bars
           if (red<256 && green>0)
           {
               myGreen= new Color(red, green, 0);
           }
           else
               getWorld().removeObject(this);
           //currPercentHP = (double) newCurrHP / hp;
           //greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
           bar.setColor(myGreen);
           bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
           bar.setColor(myGrey);
           bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
           //greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
           this.setImage(bar);
        }
}
danpost danpost

2017/3/26

#
The 'hit' method does not belong in the HealthBar class; nor does the 'damage' field.
ironphoenix20 wrote...
also, my bar is currently starting as a grey color completely. can you see why this is? it should be entirely green at the beginning.
Probably because you commented out lines 140 and 141.
ironphoenix20 ironphoenix20

2017/3/27

#
ok heres my current code. i want the green/red bar to decrease completely and then when it reaches 0, increase back up. but right now, it just goes to th end but does not increase back up. any ideas why?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * HealthBar with two sections to show HP remaining
 * left section decreases and increases in size and
 * changes gradually from green to red to show
 * remaining HP percentage and grey section to show HP lost
 * 
 *
 * Accepts any size - use Constants

 *
 * @author Deejesh Subramanian and Tishko Araz
 * @version 0.0.1 (2017)
 */
public class HealthBar extends Actor
{
    private double hp = 100;
    private int currHp = 100;
    private double damage = 5;
    private double xCoor;
    private double yCoor;
   
    // Declare Instance Variables
   
    private double currPercentHP;
    private int greenBarSize=255;
 
    // Declare Instance Images
    private GreenfootImage bar;
    private GreenfootImage greyBar;
   
    // Some constants - can be changed to suit size of related objects
    private final int HP_BAR_WIDTH = 255;
    private final int HP_BAR_HEIGHT = 20;
    private final int OFFSET = 36;
 
    // Declare Instance Objects
    private Actor target;
 
    // Declare some Color objects
    private int red=0;
    private int green=255;
    private Color myGreen = new Color (red, green, 0);
    private Color myRed = new Color (255, 0, 0);
    private Color myGrey = new Color (169, 169, 169);
 
    /**
     * Main constructor - creates a new bar image that is default grey and sets it to the HealthBar actor
     */
    public HealthBar()
    {
        bar = new GreenfootImage(HP_BAR_WIDTH, HP_BAR_HEIGHT);
        greyBar = new GreenfootImage(100,100);
        bar.setColor(myGrey);
        bar.fill();
        //bar.setColor(myGrey);
        //bar.fill();
        //greyBar.setColor(myGrey);
        //greyBar.fill();
        /*if (currHP == maxHP)
        {*/
        this.setImage(bar);
        //this.setImage(greyBar);
        }
        /*else
        {
            this.setImage(bar);
        }*/
   
 
    /**
     * Constructor that takes one int - for objects starting with max hit points.
     * This will set both current and maximum hit points to the same value.
     * 
     * @param inMaxHP the maximum hit points the actor is allowed to have
     */
    public HealthBar(int inMaxHP)
    {
        this(); // Calst he Main constructor (above)
        hp = inMaxHP;
        currHp = inMaxHP;
    }
 
    /**
     * Constructor takes an int for current and max hitpoints and also takes in an
     * Actor, which this HP Bar will follow - Whenever the Actor moves, so will this
     * hp bar. If the Actor is removed from the World, this HP bar will destroy itself.
     *
     * NOTE: This is the Constructor used in the Bug simulation
     * 
     * @param inMaxHP the maximum hit points the actor is allowed to have
     * @param target the Actor the HealthBar is associated with
     */
    public HealthBar (int inMaxHP, Actor target)
    {
        this(inMaxHP);
        this.target = target;
    }
 
    /**
     * Constructor that takes in a different value for current and max HP, ideal for
     * when a new health bar is needed for an Actor that doesn't have full HP.
     * 
     * @param inMaxHP the maximum hit points the actor is allowed to have
     * @param inCurrHP the current HP of an actor that starts out with less than max HP
     */
    public HealthBar(int inMaxHP, int inCurrHP)
    {
        this();
        hp = inMaxHP;
        currHp = inCurrHP;
    }
 
    /**
     * 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()
    {
        /*if (target.getWorld() != null)
        {
            setLocation (target.getX(), target.getY() - OFFSET);
        }
        else
            getWorld().removeObject(this);*/
        //setLocation(200,200);
        update();
    }    
 
    /**
     * update Method:
     *
     * Expects new current HP
     *
     * changes length and color of green section of bar based
     * on new current HP
     */
    public void update ()
    {
       if (hit()&&green>0){
           decrease();
        }
        //else if (hit() && greenBarSize<=100)
        //{
          //  increase();
        //}
       if (heal()&&green==0){
           increase();
        }
           //return true;
       
       if (currHp<=20){
       
       
        }
        /*// Don't do anything if current HP hasn't changed
        if (newCurrHP != currHP)
        {
            if (newCurrHP == maxHP)
            {
                this.setImage(blank);
                //return false;
            }
            else
            {
               
                // Redraw HP bar w/ appropriate amount of green and red based on
                // current HP divided by max HP
                currHP = newCurrHP;
                currPercentHP = (double) currHP / maxHP;
                greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
                redBarSize = HP_BAR_WIDTH - greenBarSize;
                //Troubleshooting code:
                //System.out.println("CurrHP: " + currHP + " curr%HP: " + currPercentHP);
                //System.out.println("GreenBar: " + greenBarSize + " RedBar: " + redBarSize);
                bar.setColor(myGreen);
                bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
                bar.setColor(myRed);
                bar.fillRect(greenBarSize, 0, redBarSize, HP_BAR_HEIGHT);
                this.setImage(bar);
                //return true;
            }
        }
        //return false;*/
    }
   
    /**
     * Checks to see if target actor has been hit and lost HP
     * 
     * @return boolean True if damage to target actor detected, otherwise false
     */
    public boolean hit()
    {//checks to see if hit by dangerous item
        return true;
    }
    
    /**
     * Returns true if target actor has been healed and gained HP 
     * 
     * @return boolean True if target actor has been healed, otherwise false
     */
    public boolean heal()
    {//checks to see if gained hp
        return true;
    }
    
    public void decrease()
    {
       red+=damage;
       green-=damage;
       greenBarSize-=damage;
       //barLength+=damage;
       //remake the bars
       if (red<256 && green>=0)
       {
            myGreen= new Color(red, green, 0);
       }
       else
            greenBarSize=0;
            //redBarSize=255;
            //increase();
       //currPercentHP = (double) newCurrHP / hp;
       //greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
       bar.setColor(myGreen);
       bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
       bar.setColor(myGrey);
       bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
       //greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
       this.setImage(bar);
    }
    
    public void increase()
    {
        red-=damage;
        green+=damage;
        greenBarSize+=damage;
        //barLength+=damage;
        //remake the bars
        if (red>=0 && green<256)
        {
            myGreen= new Color(red, green, 0);
        }
        else
           greenBarSize=255;
           //decrease();
        //currPercentHP = (double) newCurrHP / hp; 
        //greenBarSize = (int) (currPercentHP * HP_BAR_WIDTH);
        bar.setColor(myGreen);
        bar.fillRect(0,0,greenBarSize, HP_BAR_HEIGHT);
        bar.setColor(myGrey);
        bar.fillRect(greenBarSize,0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
        //greyBar.fillRect(greenBarSize, 0, HP_BAR_WIDTH-greenBarSize, HP_BAR_HEIGHT);
        this.setImage(bar);
    }
}
danpost danpost

2017/3/27

#
You still seem to want to put 'heal' and 'hit' methods in this class. They do not belong here. The only thing a healthbar does is have its value and image adjusted. Instead of calling 'hit' and 'heal' from outside the class, you should be calling a method like 'adjustHealth(int amount)' where the value is adjusted by the given amount and the image is adjusted to reflect that new value. So, in this class, you would have (1) the constructors; (2) a 'private void updateImage()' method; (3) a 'public void adjustValue(int)' method; and (4) a 'public int getValue()' method. Have you taken a look at my Value Display Tutorial scenario yet? It shows exactly what is needed.
There are more replies on the next page.
1
2
3