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

2016/5/6

Health isn't removing

ZeroSeven ZeroSeven

2016/5/6

#
So I've created a health bar and have set everything up so that it should work but when my character encounters an enemy nothing happens. I think it is because of the code in the health bar class because when I just have it preform the remove code action on its own nothing happens.
public class HealthBar extends Actor
{
    int health = 4;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    /**
     * Act - do whatever the HealthBar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public HealthBar()
    {
        update();
    }
    
    public void act() 
    {
        update();
    } 
    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
    }
    public void loseHealth()
    {
        health--;
    }
}
public class Lavnar extends Mover
{
    boolean touchingSamus  = false;
    /**
     * Act - do whatever the lavnar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        lookForSamus();
        followSamus();
        killSamus();
    }
    public void lookForSamus()
    {
        if (isAtEdge())
       {
           turn(180);
       }
    }
    public void killSamus()
    {
        if (isTouching(Samus.class))
        {
            World myWorld = getWorld();
            LavaLand lavaLand = (LavaLand)myWorld;
            HealthBar healthbar = lavaLand.getHealthBar();
            if(isTouching(Samus.class) == false)
            {
                healthbar.loseHealth();
                touchingSamus = true;
                 if(healthbar.health <=0)
                 {
                     removeTouching(Samus.class);
                     Greenfoot.stop();
                     LavaLand w = (LavaLand) getWorld();
                     w.showEndMessage();
                } 
            }
        } else {
            touchingSamus = false;
        }
    }
    public void followSamus()  
    {  
        int dist = 1000;  
        Mover closest = null;  
           
        if(!getObjectsInRange(dist, Samus.class).isEmpty())  
        {  
        for (Object obj: getObjectsInRange(dist, Samus.class))  
        {  
        
            Mover Samus = (Mover) obj;  
            move(4);
            int SamusDist = (int) Math.hypot(Samus.getX() - getX(), Samus.getY() - getY());  
            if (closest == null || SamusDist< dist)  
            {  
                    closest = Samus;  
                    dist = SamusDist;  
            }  
        }  
        turnTowards(closest.getX(),closest.getY());  
        }     
    }

}
public class LavaLand extends World
{
    ScoreCounter scoreCounter = new ScoreCounter();
    HealthBar healthBar = new HealthBar();
    GreenfootSound backgroundMusic = new GreenfootSound("sandstorm.mp3");
    /**
     * Constructor for objects of class LavaLand.
     * 
     */
    public LavaLand()
    {    
          super(800, 600, 1); 
          showName();
          showControl();
          Samus mySamus = new Samus();
          addObject(mySamus, 400, 300);
          addObject(scoreCounter, 100, 25);
          addObject(healthBar, 200, 25);
          
    }
    public ScoreCounter getScoreCounter()
    {
        return scoreCounter;
    }
    public HealthBar getHealthBar()
    {
        return healthBar;
    }
}
danpost danpost

2016/5/6

#
The only thing that immediately catches my attention is that you are having the health lost when NOT touching a Samus object (line 28 of the Lavnar class). I do not currently see any reason for having the 'touchingSamus' field in the Lavnar class. Its value is being altered within the class; but, other than that, it does not appear to be used anywhere which warrents its existence in your code. Lines 36 and 37 of the Lavnar class can be simplified. Line 36 is not needed as a reference to the LavaLand world is already available in the 'lavaLand' variable (line 26). Just change 'w' in line 37 to 'lavaLand'. Also, if you change 'myWorld' in line 26 to 'getWorld()', you can remove line 25. Now, back to issues with the healthbar. I do not think there is any problem with the code of the HealthBar class. However, I would not update the image every act cycle, but only when the value is changed. Remove the 'act' method and change the 'loseHealth' method to:
public void loseHealth()
{
    health--;
    update();
}
If all it takes is four act cycles of touching Samus to lose all health, then, from what I can tell, the healthbar should pretty much instantly drop to zero and the Samus object will be removed. If this is not what is happening, please describe what current behavior is not wanted so that we can hopefully fix down where the problem resides in the code. Otherwise, it would be working properly, so far and you would need to continue by adding a delay timer, which would be a good replacement for the 'touchingSamus' field (if the timer field was zero, then -- no, not touching Samus; if it was some positive value, then touching Samus and the field can be decremented). Only decrease the health bar value when the field has a zero value. And, at that time, set the field to the number of act cycles to wait before decreasing the health again.
You need to login to post a reply.