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

2017/4/7

can someone check my code for my health bar?

greenaliens greenaliens

2017/4/7

#
making a simulation and want al my swimmer instances to have a health bar. World is not constructing but everything complies fine. I am not done yet since i havent made the health bar work yet. but i atm i just need the world to construct.
for swimmer class 
[code]for swimmer class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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


    protected int numberOfSwimmers=0;
    protected int speed;
    public boolean stop;
    protected boolean isHurt;
    //health bar varibales 
    private int Heal = 20;
    private int damage = 0 ; 
    private int HP; 
    private int maxHP; 
    private HealthBar h; 
    private int offset = 10;
    
    public Swimmer()
    { 
       maxHP = 2000;
       HP= maxHP;
       h = new HealthBar (HP, this);
      
        
    }
    
     public int getHP()
   {
       return maxHP;
   }
   
   public void addedToWorld (World beach)
   {
        
       beach.addObject(h, getX(), this.getImage().getHeight()/2 +offset + getY()); 
       
   }


   

    /**
     * Act - do whatever the Swimmer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move();
        checkEdge();
    }    

    public void move ()
    {
        if (Greenfoot.getRandomNumber(100)<2)
        {
            turn (Greenfoot.getRandomNumber(180));
        }
        move(speed);
    }

    public void checkEdge()
    {
        if ((getX() == 959)||(getX() == 0))
        {
            getWorld().removeObject(this);
        }
        else if ((getY() == 639) ||(getY() == 0))
        {
            getWorld().removeObject(this);
        }

    }

    
   public void checkCollision()
    {
        Actor shark = getOneIntersectingObject(Shark.class);
        if (shark!= null)
        {
           //damageTaken(5);
           isHurt = true;
           stop = true;
           //shark.checkTouchSwimmer();
        }
        Actor jellyFish = getOneIntersectingObject(JellyFish.class);
        if (jellyFish!= null)
        {
            //damageTaken(1);
            isHurt = true;
            stop = true;
        }
        Actor seaStar = getOneIntersectingObject(Seastar.class);
        if (seaStar!= null)
        {
            //healed(5);
        }

    }
    
    public boolean getIsHurt()
    {
        return isHurt;
    }
    public void updateCondition(boolean stop, boolean isHurt)
    {
        this.stop = stop;
        this.isHurt = isHurt;
    }
}
world class 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Beach here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Beach extends World
{
    private int swimmerSpawnRate= 50;
    private HealthBar h= new HealthBar(1);
    private Swimmer s;
    World beach;
    /**
     * Constructor for objects of class Beach.
     * 
     */
    public Beach()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(960, 640, 1); 
        addObject (new Lifeguard(), 25,50);
        addObject (new Lifeguard(), 75, 50);
        addObject (new Lifeguard(), 125, 50);
        addObject (new Shark(), 300, 200);
    }

    public void act()
    {
        swimmerSpawnRate--;
        if (swimmerSpawnRate==0)
        {
            int randX= Greenfoot.getRandomNumber(960);
            int randY= Greenfoot.getRandomNumber(640);
            int swimmer= Greenfoot.getRandomNumber(2);
            if (swimmer==1)
            {
                addObject(new Swimmer1(),randX,randY); 
                swimmerSpawnRate=10;
            }
            else 
            {
                addObject (new Swimmer2(), randX, randY);
                swimmerSpawnRate=10;
            }
        }

    }

    //copy from Mr.Cohen's code
    public static float getDistance (Actor a, Actor b)
    {
        double distance;
        double xLength = a.getX() - b.getX();
        double yLength = a.getY() - b.getY();
        distance = Math.sqrt(Math.pow(xLength, 2) + Math.pow(yLength, 2));
        return (float)distance;
    }

}
for healthbar class 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HealthBar extends Swimmer
{
    //instance variables 
    private int maxHP; 
    private int currentHP=0;
    private int barWidth = 50;
    private int barHeight = 5;
    private final int offset= 10; 
    //instance image 
    private GreenfootImage myImage;
    private Actor target; 
    //colors 
    private Color green = new Color (0,255,0);
    private Color red = new Color (255,0,0);

    private HealthBar h;
    private int totalDamage= 0 ;
    private int totalHPBack= 0 ; 
    //consstructs the heath bar 
  
    public HealthBar(int inputHP, Actor target) 
    {
        this(inputHP); 
        this.target= target; 
        update();
    }
     public HealthBar (int inputMaxHP)
    {
        //sets the maxHP of healthbar to the max hp of actor
        maxHP=inputMaxHP;
       
        //creates the healthbar
        update();
    }
        
    public void update ()
    {
        myImage = new GreenfootImage(barWidth,barHeight);

        this.setImage(myImage);
        myImage.setColor(green);
        myImage.fill();

        totalDamage = 0;
        totalHPBack = 0;
    }

    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if ((target.getWorld()!= null)&&(getWorld()!=null))
        {
            setLocation (target.getX(), target.getY() - offset );
        }
        else 
        {
            getWorld().removeObject(this);
        }

    }    
}
danpost danpost

2017/4/7

#
One big problem is that the HealthBar class should not extend the Swimmer class. Change it to extend the Actor class.
You need to login to post a reply.