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

Comments for Progress bar/Health bar class

Return to Progress bar/Health bar class

A new version of this scenario was uploaded on Thu Feb 09 09:50:44 UTC 2012 Still fully functional, with added support and flexibility.
GazzzahGazzzah

2012/2/22

Can I scab some of this code? Do you want me to reference you in any particular way?
danpostdanpost

2012/2/23

Yes, you may. That was the purpose of creating it; so other may benefit from it. As far as referencing me, well, it would be nice (but not absolutely neccessary)! Voting is optional, also.
GazzzahGazzzah

2012/2/25

It works beautifully!
danpostdanpost

2012/2/25

@Gazzzah, thanks. I am happy you are pleased with it.
Great! I myself had my own health bar but this is really nicely done, providing plenty of flexibility. This deserves a vote, and it got mine.
danpostdanpost

2012/6/8

@limefortheworld, ty for the review (and the vote).
when you click the bar and then click the "add new bar" and then click in the world to add it there. A text field comes up, what do I put in there?
danpostdanpost

2012/6/27

The constructor for the bar takes two Strings and two ints. - 1st string: name of bar (your choice; mine says Player 1) - 2nd string: unit of measure for what the bar is used (mine says Health Points) - 1st int: the initial value of the bar (mine starts at 25) - 2nd int: the maximum value of the bar (initially set at 100) I will check how well documented it is, and if not satisfactory, will update it.
I put what you have down into the text field and i got error: ';' expected i put ';' into the last one and it still said error and then in all of them and still got error.
danpostdanpost

2012/6/27

The strings need to be in double quotes. Try 1st string: "Bar 1" 2nd string: "points" 1st int: 0 2nd int: 50
A new version of this scenario was uploaded on Wed Jun 27 22:31:49 UTC 2012 Added Bar class method comments.
bg.setColor(Color.WHITE) error cannot find symbol - variable Color how do i fix not sure why its giving me an error on mine but yours works perfectly
erdelferdelf

2012/6/28

you have to import java.awt.Color;
BytelordBytelord

2012/7/5

Good work, i will use this for my project. One thing you have to change: You must have the ability to change width and height per constructor!
BytelordBytelord

2012/7/5

So with this it would be perfect public Bar(String refText, String unitType, int initValue, int maxValue, int barw, int barh) { referenceText = refText; unitOfMeasure = unitType; maximumValue = maxValue; barWidth = barw; barHeight = barh; add(initValue); }
CockyCocky

2012/8/30

für was soll das sein?
danpostdanpost

2012/8/30

@Cocky, english please (ich nicht verstehe)
danpostdanpost

2012/8/30

@Bytelord, I felt it best to leave that to the sub-classes. If you only have one size bars in your scenario, just change the defaults (it is the code that I request not be change; but I do feel that anyone who uses this should at least be able to set the default values).
PoliPoli

2013/1/18

cool
AD12AD12

2013/2/18

just started programmimng a game like space invadors meets stargate (i found already one motivating scenario) and my intention is to write the code by my self, but now i want to add a bar for the shots but i got no idea how to start and i can't find something helpfully. Do you know where to look up? i cant do something with your code...
danpostdanpost

2013/2/18

A Bar object is pretty much like a Counter object in that it has a numeric value and displays it on the screen. The difference between the two is how the value is displayed. The basic content of this kind of class is: - a field to hold the value - a method to create the bar image (call it 'updateImage') called from the constructor and any method that changes the bar value - a method to return the value (call it 'getValue') - one or more methods to adjust the bar value (ex. 'add', 'subtract', 'setValue', 'increment', 'decrement') An example of a draft of a simple 'updateImage' method can be found at the following location: http://www.greenfoot.org/topics/find/16886#post_16886
AD12AD12

2013/2/18

Ok Thank you. And what exactly is this field to hold the value? Its no image right!? btw: my Ship (Ship.class) creates the Shots.. (Shot.class) if i adjust the barvalue in the ShotBar (ShotBar.class), how can i return the value into my Ship class, to stop shooting?
danpostdanpost

2013/2/18

@AD12, please refer to [url=http://docs.oracle.com/javase/tutorial/java/index.html]Learning the Java Language[/url] for information on fields et al.
AD12AD12

2013/2/27

i managed to create the bar with the method updateImage() but i cant return the field, the value (that shows how much the bar should get filled) is in. in class1 i got public int getValue1() { return Value1; } and in class 2 i create an object of class1 and try to get the value: public void act() { class1 cl= new class1(); Value = cl.getValue1(); } it works when i write "return Value1 = 10;" but it doesnt return the field? btw is my programm really slow and doesn't work roundly, could it be a problem of my code?
danpostdanpost

2013/2/27

Every act cycle you are creating a NEW class1 object (it would not take long for this to slow down your scenario) and then getting the initial value of the newest object created. Do not create the class1 object in the act method. You should probably have an instance object field for the bar object; and you can create the object while you declare the field with the following (in class2): private class1 c1 = new class1(); Next, for testing purposes, change your act method to the following (in class2): public void act() { if ("up".equals(Greenfoot.getKey()) { c1.add(1); System.out.println(""+c1.getValue1()); } } Make sure you add this method to the class1 code: public void add(int amount) { Value1 += amount; updateImage(); } Now, compile the scenario, start it and press the "up" arrow key a few times to test it out. If you continue to have problems, start a new discussion thread and post the class1 code with a description of how it is not doing what you want.
Have one, too. But I didn't make all these methods. Nice work.
JetLennitJetLennit

2013/4/20

Could you add a discussion with the methods that you can use?
danpostdanpost

2013/4/20

@JetLennit, just download the project and open in Greenfoot. Then open the 'Bar' class code in 'Documentation' view.
JetLennitJetLennit

2013/4/20

Okay, thank you!
ndrwdslvndrwdslv

2013/4/21

Very detailed and thorough support class.. good job!
danpostdanpost

2013/4/21

@ndrwdslv, thank you. You might want to check out my Bar Sub-classes scenario which demonstrates different ways to give it functionality. It is at the following URL: http://www.greenfoot.org/scenarios/5677
Liamb2179Liamb2179

2014/4/22

Having some trouble adding the health bar to the world from a non-world class. Specifically I am making a game where you can create units and want the health bar to be created at that units x and y coordinates and follow them but I am getting a null pointer exception.
danpostdanpost

2014/4/22

@Liamb2179, please start a discussion thread on this issue. Supply the code you are using to add the bar into the world as well as code around where the error occurs. Also, copy/paste the error message in its entirety.
Liamb2179Liamb2179

2014/4/22

I figured it out based on a different discussion thread you answered haha, I was trying to add the bar object into the world from the constructor of the unit.
danpostdanpost

2014/4/23

@Liamb2179, it is possible to do so; but, the world object must be passed to the constructor (directly or indirectly) like a method argument. What I mean by indirectly, is an Actor object ( 'Actor actor' )that is in the world could be passed instead because it has a reference to the world that can be retrieved with 'actor.getWorld()'.
SullyFishSullyFish

2014/5/14

how would I make the bar go down by making a different class touch a different class?
SullyFishSullyFish

2014/5/14

it doesnt find bar.subtract in my other class
SullyFishSullyFish

2014/5/14

public void hit(){ Actor actor = getOneObjectAtOffset(0, 0, Ennemies.class); if(actor != null) { Bar.subtract(1); } }
danpostdanpost

2014/5/14

@SullyFish, start a discussion thread, show how you create the bar and how you are trying to use the bar (code posting is better in discussion threads).
SullyFishSullyFish

2014/5/14

nvm sorry I found it
A new version of this scenario was uploaded on 2017-01-20 23:18:50 UTC
A new version of this scenario was uploaded on 2017-10-16 02:02:20 UTC
Nate2002Nate2002

2018/11/29

This is pretty cool
data17data17

2019/1/9

public Bar useFul = new Bar("danpost", "thank you!", 100, 100); // easy peasy :-)
AbwattsAbwatts

2019/4/1

Thanks! Super easy and simple to use! Respect =]
A new version of this scenario was uploaded on 2020-11-02 13:39:40 UTC
Roshan123Roshan123

2021/4/16

I have a question I added some tags in one of my scenario before 2-3 weeks and now I noticed that my all tags are removed except 2 I chaged my tags in mobile and after that i updated the same scenario in computer After 2-3 week i noticed its not their except 2 How is it possible??? Is my account is being used by any other user or its not something like i m thinking abt
danpostdanpost

2021/4/16

@Roshan123, how is this related to this scenario? ... start a new discussion thread under "Other". If I have anything to say about it, I will respond there.
Roshan123Roshan123

2021/4/17

@danpost...ok i created it