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

2013/3/23

Heath Bar

1
2
GrimCreeper312 GrimCreeper312

2013/3/23

#
What would be the best way to make a heath bar in greenfoot.
danpost danpost

2013/3/23

#
There are several scenarios with support classes on the site for a health bar. My is called Progress bar/Health bar. Download the scenario and either copy/paste the code into a newly created actor class in your scenario or copy the Bar files into your scenario folder. You can review my scenario to see how to implement it into yours.
GrimCreeper312 GrimCreeper312

2013/3/23

#
I am using danpost's bar. i get an error. here is the code that has the error
public ScoreBar bar2 = new ScoreBar("","Score",0, roundScore);
i need it so that the maximum value changes with the roundscore
danpost danpost

2013/3/24

#
My bar class is not called 'ScoreBar', it is called 'Bar'. and the parameters do not look right for my Bar class either. Are you sure that is my Bar class?
GrimCreeper312 GrimCreeper312

2013/3/24

#
yes i changed the name to score bar; i want to use a varible as the maximum value roundscore.
GrimCreeper312 GrimCreeper312

2013/3/24

#
the orignal that i have is
public Bar bar = new Bar("Heath","points",0, 100);  
danpost danpost

2013/3/24

#
There is not need to change the name of the Bar class. You can create multiple bars as follows:
Bar scoreBar = new Bar("Score", "points", 0, roundscore);
Bar healthBar = new Bar("Health", "points", 0, 100);
As long as 'roundscore' is assigned a value greater than zero, you should not get an error.
GrimCreeper312 GrimCreeper312

2013/3/24

#
i did what u said at it gave me an error. if it means anything roundScore is static;
java.lang.ArithmeticException: / by zero
	at ScoreBar.newImage(ScoreBar.java:56)
	at ScoreBar.add(ScoreBar.java:110)
	at ScoreBar.<init>(ScoreBar.java:48)
	at Mw.<init>(Mw.java:13)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
	at greenfoot.core.Simulation.newInstance(Simulation.java:578)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:408)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:465)
	at greenfoot.core.Simulation.maybePause(Simulation.java:279)
	at greenfoot.core.Simulation.runContent(Simulation.java:210)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.NullPointerException
	at greenfoot.World.addObject(World.java:392)
	at Mw.prepare(Mw.java:82)
	at Mw.<init>(Mw.java:33)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
	at greenfoot.core.Simulation.newInstance(Simulation.java:578)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:408)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:465)
	at greenfoot.core.Simulation.maybePause(Simulation.java:279)
	at greenfoot.core.Simulation.runContent(Simulation.java:210)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
	at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867)
	at All.canSee(All.java:47)
	at Player.checkCollision(Player.java:63)
	at Player.act(Player.java:35)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
	at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867)
	at All.canSee(All.java:47)
	at Player.checkCollision(Player.java:71)
	at Player.act(Player.java:35)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.ArithmeticException: / by zero
	at Bar.newImage(Bar.java:56)
	at Bar.add(Bar.java:110)
	at Bar.<init>(Bar.java:48)
	at Mw.<init>(Mw.java:13)
	at PlayGame.checkClick(PlayGame.java:24)
	at PlayGame.act(PlayGame.java:17)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.ArithmeticException: / by zero
	at Bar.newImage(Bar.java:56)
	at Bar.add(Bar.java:110)
	at Bar.<init>(Bar.java:48)
	at Mw.<init>(Mw.java:13)
	at PlayGame.checkClick(PlayGame.java:24)
	at PlayGame.act(PlayGame.java:17)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.ArithmeticException: / by zero
	at Bar.newImage(Bar.java:56)
	at Bar.add(Bar.java:110)
	at Bar.<init>(Bar.java:48)
	at Mw.<init>(Mw.java:13)
	at PlayGame.checkClick(PlayGame.java:24)
	at PlayGame.act(PlayGame.java:17)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.ArithmeticException: / by zero
	at Bar.newImage(Bar.java:56)
	at Bar.add(Bar.java:110)
	at Bar.<init>(Bar.java:48)
	at Mw.<init>(Mw.java:13)
	at PlayGame.checkClick(PlayGame.java:24)
	at PlayGame.act(PlayGame.java:17)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.ArithmeticException: / by zero
	at Bar.newImage(Bar.java:56)
	at Bar.add(Bar.java:110)
	at Bar.<init>(Bar.java:48)
	at Mw.<init>(Mw.java:13)
	at PlayGame.checkClick(PlayGame.java:24)
	at PlayGame.act(PlayGame.java:17)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
danpost danpost

2013/3/24

#
You probably did everything EXCEPT make sure that 'roundscore' has a value greater than zero (like I said above).
GrimCreeper312 GrimCreeper312

2013/3/24

#
roundScore = 500;
danpost danpost

2013/3/24

#
Please show your 'newImage' method in your ScoreBar class and specify which line is 56 in that class. Whatever you are dividing by in that line is equal to zero, which causes the error.
GrimCreeper312 GrimCreeper312

2013/3/24

#
 /**
     * Method 'newImage': builds a new image for the bar, determined by the values set for it
     */
    private void newImage()
    {
        int barValue = (int) (barWidth * (value - minimumValue) / (maximumValue - minimumValue));
        GreenfootImage leftImg = new GreenfootImage(referenceText + " ", (int) fontSize, textColor, backgroundColor);
        GreenfootImage rightImg = (showTextualUnits) ? new GreenfootImage(" " + value + " " + unitOfMeasure, (int) fontSize, textColor, backgroundColor) : new GreenfootImage(1, 1);
        int maxX = (leftImg.getWidth() > rightImg.getWidth()) ? leftImg.getWidth() : rightImg.getWidth();
        GreenfootImage barImg = new GreenfootImage(barWidth + 4, barHeight + 4);
        barImg.setColor(backgroundColor);
        barImg.fill();
        barImg.setColor(textColor);
        barImg.drawRect(0, 0, barImg.getWidth() - 1, barImg.getHeight() - 1);
        if (value > minimumValue)
        {
            if (breakLow)
            {
                if (value > (usingBreakValue ? breakValue : (int) (breakPercent * (maximumValue - minimumValue) / 100 + minimumValue))) barImg.setColor(safeColor);
                else barImg.setColor(dangerColor);
            }
            else
            {
                if (value < (usingBreakValue ? breakValue : (int) (breakPercent * (maximumValue - minimumValue) / 100 + minimumValue))) barImg.setColor(safeColor);
                else barImg.setColor(dangerColor);
            }
            if (value == (usingBreakValue ? breakValue : (int) (breakPercent * (maximumValue - minimumValue) / 100 + minimumValue)))
            {
                int r = (int) ((safeColor.getRed() + dangerColor.getRed()) / 2);
                int g = (int) ((safeColor.getGreen() + dangerColor.getGreen()) / 2);
                int b = (int) ((safeColor.getBlue() + dangerColor.getBlue()) / 2);
                barImg.setColor(new Color(r, g, b));
            }
            barImg.fillRect(2, 2, barValue, barHeight);
        }
        int sumX = 2 * maxX + barImg.getWidth();
        int maxY = 0;
        if (leftImg.getHeight() > maxY) maxY = leftImg.getHeight();
        if (barImg.getHeight() > maxY) maxY = barImg.getHeight();
        if (rightImg.getHeight() > maxY) maxY = rightImg.getHeight();
        GreenfootImage image = new GreenfootImage(sumX, maxY);
        image.setColor(backgroundColor);
        image.fill();
        image.drawImage(leftImg, maxX - leftImg.getWidth(), (image.getHeight() - leftImg.getHeight()) / 2);
        image.drawImage(barImg, maxX, (image.getHeight() - barImg.getHeight()) / 2);
        image.drawImage(rightImg, maxX + barImg.getWidth(), (image.getHeight() - rightImg.getHeight()) / 2);
        setImage(image);
    }
GrimCreeper312 GrimCreeper312

2013/3/24

#
where should i start the line count
GrimCreeper312 GrimCreeper312

2013/3/24

#
should i count coments
danpost danpost

2013/3/24

#
In your main scenario window, on the menu bar, select 'Edit>Preferences' and check the 'Display line numbers' checkbox and click on the 'OK' button. Then open the ScoreBar class and find line 56.
There are more replies on the next page.
1
2