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.
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.
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);
}
@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).
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...
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
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?
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?
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.
@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
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.
@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.
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.
@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()'.
@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).
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
@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.
2012/2/22
2012/2/23
2012/2/25
2012/2/25
2012/6/7
2012/6/8
2012/6/27
2012/6/27
2012/6/27
2012/6/27
2012/6/28
2012/6/28
2012/7/5
2012/7/5
2012/8/30
2012/8/30
2012/8/30
2013/1/18
2013/2/18
2013/2/18
2013/2/18
2013/2/18
2013/2/27
2013/2/27
2013/4/7
2013/4/20
2013/4/20
2013/4/20
2013/4/21
2013/4/21
2014/4/22
2014/4/22
2014/4/22
2014/4/23
2014/5/14
2014/5/14
2014/5/14
2014/5/14
2014/5/14
2018/11/29
2019/1/9
2019/4/1
2021/4/16
2021/4/16
2021/4/17