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

2018/7/2

My extra life doesn't work

grnft grnft

2018/7/2

#
I've created a Healthbar, but it only works when my objects hits another. So while my actor is unprotected, he has only one life. But as soon as he hits the other object, he has two lifes. So he hits the joker which gives him an extra life. Well, it doesn't work so far. Here's my code so far: The class of my main character:
if (isTouching(Stein.class))
        {
            removeTouching(Stein.class);
            Actor dementor = getOneIntersectingObject(Dementor.class);
            if(dementor!=null)
            {
                World myWorld = getWorld();
                Hogwarts hogwarts = (Hogwarts)myWorld;
                HealthBar2 healthbar2 = hogwarts.getHealthBar2();
                if(touchingDementor == false)
                {
                    healthbar2.loseHealth();
                    touchingDementor = true;
                    if(healthbar2.health <= 0)
                    {
                        myWorld.removeObject(this);
                    }
                }

            }
            else
            {
                touchingDementor = false;
            }

        }
The class of my Healthbar:
public class HealthBar2 extends Actor
{
    int health = 2;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
     
    public HealthBar2()
    { 
        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--;
     }
danpost danpost

2018/7/2

#
grnft wrote...
I've created a Healthbar, but it only works when my objects hits another. So while my actor is unprotected, he has only one life. But as soon as he hits the other object, he has two lifes. So he hits the joker which gives him an extra life. Well, it doesn't work so far. Here's my code so far: << Codes Omitted >>
I do not see any codes for the following: * in HealthBar2 class: ability for a health bar to increase value; * in main character class: anything that attempts to increase the value of its health; Also, your health bar imaging is messed up. The width is 80 and pixels per health is set to width divided by health, which is initially set to 2; so pixels per health is 40 (half the width of the bar). So, the bar will only show health of zero (no health shown), one (half of full health shown) and "greater than one" (full health shown). If you are to allow for additional health, you will need to decide either on a maximum health value and use it to compute pixels per health or find another way, other than a health bar, to show the health value. Maybe you could modify the bar somehow to show extra health. If using a maximum value, it is best to compute the number of pixels to use when building (updating) the bar image -- especially if the maximum does not evenly divide the width of the bar. Another suggestion is to update the image only initially and when the value is changed. There is no need to create the same image over and over again each act cycle when the value stays the same.
grnft grnft

2018/7/2

#
Thanks for improving my healthbar! But my question was how to make my healthbar work, so that my actor doesn't die when he got shot by the enemy. But only then, when he hit another object which gives him the extra life. And I don't want my actor to gain health, he only looses it. The code inside the if-clause works at another actor, so there's only a problem with the if-clause...
danpost danpost

2018/7/2

#
grnft wrote...
my question was how to make my healthbar work, so that my actor doesn't die when he got shot by the enemy. But only then, when he hit another object which gives him the extra life. And I don't want my actor to gain health, he only looses it. The code inside the if-clause works at another actor, so there's only a problem with the if-clause...
I think your current code confused me. Maybe you can explain what a Stein object is -- I am quite sure a Dementor object is an enemy. Also, explain why you would have to touch both at the same time to lose health (if not obvious by what a Stein object is). It even seems strange that you would have to touch another Stein object before being able to lose more health.
grnft grnft

2018/7/2

#
My actor Harry Potter touches the Resurrection Stone (here it's "Stein"). As soon as he touches it, the next time, he hits a dementor (the enemy), he doesn't die for once. The Resurrection Stone gives him another life, he functions as a joker. The actor only looses health when he hits a dementor but the Stone "enables the second life".
danpost danpost

2018/7/2

#
grnft wrote...
My actor Harry Potter touches the Resurrection Stone (here it's "Stein"). As soon as he touches it, the next time, he hits a dementor (the enemy), he doesn't die for once. The Resurrection Stone gives him another life, he functions as a joker. The actor only looses health when he hits a dementor but the Stone "enables the second life".
Then I was correct that both a Stein and a Dementor need not touch simultaneously to lose health. For example:
// with field
boolean hasExtraLife;

// action code
if (isTouching(Stein.class))
{
    removeTouching(Stein.class);
    hasExtraLife = true;
}
if (isTouching(Dementor.class))
{
    if (!touchingDementor)
    {
        touchingDementor = true;
        if (hasExtraLife) hasExtraLife = false;
        else
        {
            HealthBar2 healthbar2 = ((Hogwarts)getWorld()).getHealthBar2();
            healthbar2.loseHealth();
            if (healthBar2.health == 0) getWorld().removeObject(this);
        }
    }
}
else touchingDementor = false;
grnft grnft

2018/7/2

#
It doesn't work:( The Dementor still kills Harry when he has hit the stone. Here's the code of the killHarry method in the Dementor class. Maybe it helps..
   public void killHarry()
    {
        Harry harry;
        if (isTouching(Harry.class))
        {
            Greenfoot.setWorld(new EndGame());
            Greenfoot.playSound("slurp.wav");
        }
    }
danpost danpost

2018/7/2

#
grnft wrote...
The Dementor still kills Harry when he has hit the stone. Here's the code of the killHarry method in the Dementor class. Maybe it helps.. << Code Omitted >>
Well, that code sure looks like it would cause the behavior you are getting.
danpost danpost

2018/7/3

#
grnft wrote...
Here's the code of the killHarry method in the Dementor class. << Code Omitted >>
Try removing the killHarry method from the Dementor class. The code in your Harry class deals with that (I believe). It just does not set an EndGame world active (yet).
grnft grnft

2018/7/3

#
Now, my code looks like this:
    public void getKilledByDementor()
    {
        if(isTouching (Dementor.class))
        {
            Greenfoot.setWorld(new EndGame());
            Greenfoot.playSound("slurp.wav");
        }
    }

    /*
     * Wenn Harry den Stein berührt, verschwindet dieser und die Funktion der Healthbar tritt in Kraft.
     */
    public void extraLife()
    {
        if (isTouching(Stein.class))
        {
            removeTouching(Stein.class);
            protecting();
        }
    }

    public void protecting()
    {
        Counter = Counter + 1;
        if(Counter >= 1000)
        {
            if (isTouching(Dementor.class))
            {
                if(!touchingDementor)
                {
                    touchingDementor = true;
                    if (hasExtraLife) 
                    {
                        hasExtraLife = false;
                    }
                    else//Der Balken wird um den in der Healthbar festgelegten Anteil dezimiert.
                    {
                        HealthBar2 healthbar2 = ((Hogwarts)getWorld()).getHealthBar2();
                        healthbar2.loseHealth();
                        if (healthbar2.health == 0) //Wenn Harry kein Leben mehr hat, verschwindet dieser, die Endwelt wird gezeigt.
                        {
                            Greenfoot.setWorld(new EndGame());
                            Greenfoot.playSound("slurp.wav");
                        }
                    }
                }
            }	
            else 
            {
                touchingDementor = false;
            }
        }
    }
The Dementor kills Harry, even if he had hit the stone which should protect him. That's all with the killing thing.
You need to login to post a reply.