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

2017/10/16

Health going down fast

monmonmon monmonmon

2017/10/16

#
Hi again, I'm having problems, my problem is when the zombie just hit me once, my health goes down all the way to zero, I'm not sure if it's with the code but here's the code:
public class Person extends Actor
{
    boolean touchingZombie = false;

public void act
{
        hitZombie();
}

public void hitZombie()
    {
        Actor zombie = getOneIntersectingObject(Zombie.class);
        Actor midget = getOneIntersectingObject(Midget.class);
        Actor explosion = getOneIntersectingObject(Explosion.class);
        if(zombie != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            HealthBar healthbar = myworld.getHealthBar();
            if(touchingZombie == false)
            {
                healthbar.loseHealth();
                touchingZombie = true;
                if(healthbar.health <=0)
                {
                    GameOver gameover = new GameOver();
                    myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                    myWorld.removeObject(this);
                }
            }
        }
        if(midget != null)
        {
             World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            HealthBar healthbar = myworld.getHealthBar();
            if(touchingZombie == false)
            {
                healthbar.loseHealth();
                touchingZombie = true;
                if(healthbar.health <=0)
                {
                    GameOver gameover = new GameOver();
                    myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                    myWorld.removeObject(this);
                }
            }
        }
           
        if(explosion != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            HealthBar healthbar = myworld.getHealthBar();
            if(touchingZombie == false)
            {
                healthbar.loseHealth();
                touchingZombie = true;
                if(healthbar.health <=0)
                {
                    GameOver gameover = new GameOver();
                    myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
                    myWorld.removeObject(this);
                }
            }
        }
        
       else {
            touchingZombie = false;
        }
        
    }
}
danpost danpost

2017/10/16

#
By setting 'touchingZombie' back to 'false' only if not touching an explosion, you are losing health while still touching a zombie or midget. Since you have multiple things that can cause loss of health and since the current code is not quite what you had intended, it is not apparent exactly what was intended. I would think that the longer the person touches any of these things, the more health would be lost; but, that would not go along with having a 'touchingZombie' field. Therefore, it appears that you want to lose health once each time one is encountered and the duration of the encounter does not matter. Using the 'touchingZombie' field for all three types of encounters, however, does not really make much sense. I would think each one, individually, should cause some loss of health. As well, it would make sense to have each zombie, and each midget, and each explosion, cause loss of health, which may not happen if a second one touches while the first is still touching. The only way to ensure that each cause loss of health exactly one time per encounter is to track the actors that are touching the person. Then each act, you would get a list of touching actors, remove previous touching actors from the list, do damage for remaining actors, then update the previous list with a new one. Something like this:
// import statement
import java.util.List;

// instance field
List<Actor> touchingActors;

// method to initialize the list (automatically called by greenfoot when person is put in a world
protected void addedToWorld(World world)
{
    touchingActors = getIntersectingObjects(Actor.class);
}

// in act method
List<Actor> touchingNow = getIntersectingObjects(Actor.class); // get list of all touching
touchingNow.removeAll(touchingActors); // remove previously touching to get list of newly touching
touchingActors = getIntersectingObjects(Actor.class); // save list off all toucching
for (Actor actor : touchingNow) // for each newly touching
{
    if (actor instanceof Zombie || actor instanceof Midget || actor instanceof Explosion)
    {
        MyWorld myWorld = (MyWorld)getWorld();
        HealthBar healthBar =  myWorld.getHealthBar();
        healthBar.loseHealth();
        if (healthBar.health <= 0)
        {
            myWorld.addObject(new GameOver(), myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
            return;
        }
    }
}
monmonmon monmonmon

2017/10/17

#
it worked, thanks again sir dan!
monmonmon monmonmon

2017/10/17

#
Another question, I also added a HealthPack actor that adds 1 health to the HealthBar, I want to know how can I make it stop gaining health when the HealthBar is full because even if the HealthBar is full, it keeps adding health.
monmonmon monmonmon

2017/10/17

#
Here's the code of the HealthBar if needed:
public class HealthBar extends Others
{
    int health = 4;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    
    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 void gainHealth()
    {
        health++;
    }
}
danpost danpost

2017/10/17

#
You just need a maximum constant field and a condition on gaining health.
monmonmon monmonmon

2017/10/17

#
Can i get an example of what you mean? sorry..
danpost danpost

2017/10/17

#
monmonmon wrote...
Can i get an example of what you mean? sorry..
// constant field
final int MAX_HEALTH = 4;

// to gain health
if (health < MAX_HEALTH) health++;
monmonmon monmonmon

2017/10/17

#
thanks
You need to login to post a reply.