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

2012/12/15

Health Bar

programmer274 programmer274

2012/12/15

#
Hey I am looking to make a simple health bar. Originally I thought about making 10 if statements for each one point of 10 health and tying each of these statements to a picture. I have realized this is kind of inefficient. Is there any way for me to tie the value of whatever the health is currently to each of the 10 pictures without 10 if statements. I was thinking about some sort of getvalue method and the number returned will find the pictures 1-10 and whatever the number is grab the picture corresponding to this value.
danpost danpost

2012/12/16

#
If you create the image programmatically, you can adjust the image depending on the value
private void updateImage()
{
    GreenfootImage image=newGreenfootImage(104, 10);
    image.drawRect(0, 0, 99, 9);
    image.fillRect(2, 2, 10*health, 6);
    setImage(image):
}
Of course, this is a very crude example. You can adjust the dimensions of each part and add Color to each part and/or the background of the image. At any rate the idea should be apparent. Just put a call to this method in the constructor of the class and in any method that changes the value of 'health' (immediately after the change, of course).
Zamoht Zamoht

2012/12/16

#
Check out my scenario feel free to use the code.
tylers tylers

2012/12/16

#
or my one: here
Zamoht Zamoht

2012/12/16

#
What is that a link to? Well I can tell it's not a scenario...
tylers tylers

2012/12/16

#
oh wrong link sorry. Heres the new one: http://www.greenfoot.org/scenarios/5114
Zamoht Zamoht

2012/12/16

#
Still... What was the other a link to? It seemed a little suspicious.
tylers tylers

2012/12/16

#
Its my minecraft server, I copied the wrong link.
You need to login to post a reply.