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

2011/12/14

HealthBar Help

DMCGames DMCGames

2011/12/14

#
I need help with a healthbar. The healthbar shows up but is not active. It will not update it self. Here is what I have been using:
public class DHealthbar extends Dragonoir
{
    /**
     * Act - do whatever the DHealthbar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        healthcheck();
        
    }   
    private void healthcheck()
    {
        if (gethealth() == 1000)
        {setImage("1000.png");}
        else  if (gethealth() == 950)
        {setImage("950.png");                                        
        }
          else  if (gethealth() == 900)
        {setImage("900.png ");                                       
        }
                else  if (gethealth() == 850)
        {setImage("850.png ");                                       
        }
                else  if (gethealth() == 800)
        {setImage("850.png ");                                       
        }
                else  if (gethealth() == 750)
        {setImage("750.png");                                        
        }
                else  if (gethealth() == 700)
        {setImage("700.png ");                                       
        }
                else  if (gethealth() == 650)
        {setImage("650.png ");                                       
        }
                
                else  if (gethealth() == 600)
        {setImage("600.png ");                                       
        }
                        else  if (gethealth() == 550)
        {setImage("550.png ");                                       
        }                else  if (gethealth()== 500)
        {setImage("500.png ");                                       
        }                else  if (gethealth() == 450)
        {setImage("450.png ");                                       
        }                else  if (gethealth() == 400)
        {setImage("400.png ");                                       
        }                else  if (gethealth() == 350)
        {setImage("350.png ");                                       
        }                else  if (gethealth() == 300)
        {setImage("300.png ");                                       
        }                else  if (gethealth() == 250)
        {setImage("250.png ");                                       
        }                else  if (gethealth() == 200)
        {setImage("200.png ");                                       
        }
                        else  if (gethealth() == 150)
        {setImage("150.png ");                                       
        }                else  if (gethealth() == 100)
        {setImage("100.png ");                                       
        }                else  if (gethealth() == 50)
        {setImage("50.png ") ;                                      
        }
        else 
        {setImage("0.png") ;      }
    }
}
and gethealth() is just
return Healthpoints;
davmac davmac

2011/12/14

#
You haven't shown any code which actually updates the Healthpoints variable. Perhaps it's not being updated. Another issue is that you seem to have an extra space in some of your filenames: {setImage("900.png "); // See the space after .png?
Duta Duta

2011/12/14

#
Also its saying when health == x amount... as in it exactly equals it. maybe the health is being affected in a way that it never is a multiple of 50. To avoid this you could do:
if(gethealth() < 25)
{setImage("0.png");
}else if(gethealth() > 25 && gethealth() <= 75)
{setImage("50.png");
}else if(gethealth() > 75 && gethealth() <= 125)
{setImage("100.png");
}else if(gethealth() > 125 && gethealth() <= 175)
{setImage("150.png");
}else if(gethealth() > 175 && gethealth() <= 225)
{setImage("200.png");
}else if(gethealth() > 225 && gethealth() <= 275)
{setImage("250.png");
}else if(gethealth() > 275 && gethealth() <= 325)
{setImage("300.png");
}else if(gethealth() > 325 && gethealth() <= 375)
{setImage("350.png");
}else if(gethealth() > 375 && gethealth() <= 425)
{setImage("400.png");
}else if(gethealth() > 425 && gethealth() <= 475)
{setImage("450.png");
}else if(gethealth() > 475 && gethealth() <= 525)
{setImage("500.png");
}else if(gethealth() > 525 && gethealth() <= 575)
{setImage("550.png");
}else if(gethealth() > 575 && gethealth() <= 625)
{setImage("600.png");
}else if(gethealth() > 625 && gethealth() <= 675)
{setImage("650.png");
}else if(gethealth() > 675 && gethealth() <= 725)
{setImage("700.png");
}else if(gethealth() > 725 && gethealth() <= 775)
{setImage("750.png");
}else if(gethealth() > 775 && gethealth() <= 825)
{setImage("800.png");
}else if(gethealth() > 825 && gethealth() <= 875)
{setImage("850.png");
}else if(gethealth() > 875 && gethealth() <= 925)
{setImage("900.png");
}else if(gethealth() > 925 && gethealth() <= 975)
{setImage("950.png");
}else
{setImage("1000.png");
}
Preferably, you would use a switch statement aswell (see here for more info).
Builderboy2005 Builderboy2005

2011/12/14

#
Alternatively, you could replace all of that code with
setImage((int)((gethealth()+25)/50)*50+".png");
I recommend using System.out.println() to see what gethealth() is returning, maybe it isn't what you expect.
AwesomeNameGuy AwesomeNameGuy

2011/12/14

#
Or you can do this:
int health; // this is updated somehow in the act method
    public void draw() {
        GreenfootImage thebar = new GreenfootImage(20,100);
        thebar.setColor(Color.RED);
        int damage = 100-health;
        thebar.fillRect(0,damage,20,100-damage);
        thebar.setColor(Color.BLACK);
        thebar.drawRect(0,0,19,99);
        setImage(thebar);
    }
That way you don't have to draw 20 different images. This creates a red health bar inside a black rectangle that is 20X100 pixels in size. Gotta import java.awt.Color; to use Color.
You need to login to post a reply.