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

2017/4/28

Health Bar Problems

DasZer0 DasZer0

2017/4/28

#
Hey Guys, A friend of mine and me used the Progress and Health Bar which is published by danpost (http://www.greenfoot.org/scenarios/4114). We included it into our Project and started using it. Now we added an object which is shooting at our main Character. We want to let the main character take damage everytime when he gets shot. We tried to do it with the subtract method but it´s still not working. We would appreciate it if one of you can help us and send us a solution
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/
 * Write a description of class feuer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class feuer extends Actor

{
    /
     * Act - do whatever the feuer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    Bar bar = new Bar("Jeff","HP",100,100);


    public void act() 
    {
        move(-8);
        hit();

    } 

    public void hit()
    {
        if(isTouching(Main.class))
        {
            bar.subtract(10);
            getWorld().removeObject(this);
        }
        else if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
    }
}
DasZer0 DasZer0

2017/4/28

#
P.S: This is the code we tried on our own ^^
Yehuda Yehuda

2017/4/28

#
I don't see very much subtracting here. Each 'feuer' has it's own bar, the 'feuer' moves left and when it hits Main or the world edge it gets removed so there is not much time for bars to decrease. Your code seems to be a bit messy. You should change it to this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Feuer here.
 * 
 * @author (your name)
 * @version (a version number or a date)
 */
public class Feuer extends Actor {

    Bar bar = new Bar("Jeff", "HP", 100, 100);

    protected void addedToWorld(World world) {
        getWorld().addObject(bar, getX(), getY() - getImage().getHeight() / 2 - bar.getImage().getHeight() / 2);
    }

    /**
     * Act - do whatever the Feuer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() {
        move(-8);
        bar.setLocation(getX(), getY() - getImage().getHeight() / 2 - bar.getImage().getHeight() / 2);
        hit();
    }

    private void hit() {
        if (isTouching(Main.class)) {
            bar.subtract(8);
        }
        if (isAtEdge() || bar.getValue() == bar.getMinimumValue()) {
            getWorld().removeObject(bar);
            getWorld().removeObject(this);
        }
    }
}
With this code the bar will stay right on top of the actor, if you want to have one stationary Bar then you shouldn't create the bar in 'Feuer', you should create it in a World then access it from 'Feuer'.
danpost danpost

2017/4/28

#
If Jeff is not created from the feuer class, then the healtbar is in the wrong class to begin with. I have a feeling Jeff is created in the Main class (being the main actor). That is where the healtbar and the collision detection with a feuer object should go.
DasZer0 DasZer0

2017/5/3

#
Bitch we got it without youuuu DU JUDE AUSM IMTERNET
You need to login to post a reply.