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

2015/4/10

Getting my health bar class to follow my Greep class

RagingAsian RagingAsian

2015/4/10

#
I want my HealthBar class to follow my Greep class once they are both created in the world, and to disappear once the greep reaches zero health. Currently, the health bar will be created in the world, but will not follow my Greep class. HealthBar class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class HealthBar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HealthBar extends Actor    
{
    boolean inPosition = false;    
    int health;
    int width=80;
    int height=15;
    int healthPerPixel;
    public HealthBar(int hp)
    {        
        health=hp;
       
        healthPerPixel=(int)(width/health);
        update();
    }
    
    public void act() 
    {
    }
        
    public void update()
    {
        setImage(new GreenfootImage(width+2,height+2));
        GreenfootImage hb=getImage();
        hb.setColor(Color.BLACK);
        hb.drawRect(0,0,width+1,height+1);
        hb.setColor(Color.GREEN);
        hb.fillRect(1,1,health*healthPerPixel,height);
    }
    
    public void loseHealth()
    {  
        //health=health-tower.dmg;
        update();
    }
    
    public void followGreep()
    {
        if (Greeps.class!=null)
        {
            int x=Greeps.getX();            
            int y=Greeps.getY();
            turnTowards(x,y);
            move(1);
            
        }else{ getWorld().removeObject(this);}
    }
}
danpost danpost

2015/4/10

#
At line 46, the condition will always be true (there is a Greeps class within the project -- it is not 'null'). At lines 48 and 49, you cannot put a class in the world and you cannot get location coordinates of a class. You can only get location coordinates of an actor object created from the Actor class. Remove the 'followGreep' method. Each Greeps object should be assigned its own healthbar object and should move its own healthbar when it moves. You can either make sure that any Greeps object removes its healthbar when it gets removed, or you can use a field in the class of the healthbar to hold its owner and have each healthbar check for the removal of its owner.
RagingAsian RagingAsian

2015/4/10

#
Could i put
Actor greep = getOneIntersectingObject(Greeps.class);
to make it work?
danpost danpost

2015/4/10

#
RagingAsian wrote...
Could i put
Actor greep = getOneIntersectingObject(Greeps.class);
to make it work?
That would not be precise. Programming is a precise art. If any other Greeps object other than the one the healthbar belongs to intersects it, it could pick up that one instead of the one it belongs to. Even if there is no chance of that happening, it is better to have each greep keep a reference to its own healthbar and move its healthbar when it moves.
RagingAsian RagingAsian

2015/4/10

#
So I should put the follow method in the specific greep object class (small,medium,and heavy, the only three classes that extends Greeps) to have the health bar follow them?
danpost danpost

2015/4/10

#
RagingAsian wrote...
So I should put the follow method in the specific greep object class (small,medium,and heavy, the only three classes that extends Greeps) to have the health bar follow them?
The only thing that should be in your Small, Medium and Heavy classes are the starting health point values (and the specific default images set to each of those classes. Everything else should be in your Greeps class. For example:
public class Small extends Greeps
{
    public Small(/** the parameter list */)
    {
        super(25, /** the rest of the parameter list */);
    }
}
where '25' is the initial health point value for Small greeps objects (for example). Like I said, the behavior of all three types of greep objects are the same, so code everything else in the Greeps class.
You need to login to post a reply.