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

2017/4/7

Healthbar

1
2
Nooooob Nooooob

2017/4/7

#
The Healthbar won't go down when X class touches Y class or rather the bar isn't updating. Healthbar
int health = 4;
    int healthWidth = 80;
    int healthHeight = 15;
    int pixelsPerHealthPoint = (int)healthWidth/health;
    public Healthbar()
    {
        update();
    }
    public void update()
    {
        setImage(new GreenfootImage(healthWidth + 2, healthHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0, 0, healthWidth + 1, healthHeight + 1);
        myImage.setColor(Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthHeight);
    }
    public void loseHealth()
    {
        health--;
        update();
    }
X class
boolean touchingY = false;
    public void act() 
    {        
        hit();
    }
    public void hit()
    {
        Actor y = getOneIntersectingObject(Y.class);
        if(y != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            Healthbar healthbar = myworld.getHealthBar();
            if(touchingY == false)
            {
               healthbar.loseHealth();
               touchingY = true;
               if(healthbar.health <= 0)
               {
                   getWorld().removeObject(this);
               }
            }
        } 
        else 
        {
                touchingY = false;
        }
    }
Can anyone help?
Nosson1459 Nosson1459

2017/4/7

#
Try changing the update() method to:
public void update() {
    GreenfootImage myImage = new GreenfootImage(healthWidth + 2, healthHeight + 2);
    myImage.setColor(Color.WHITE);
    myImage.drawRect(0, 0, healthWidth + 1, healthHeight + 1);
    myImage.setColor(Color.RED);
    myImage.drawRect(1, 1, health * pixelsPerHealthPoint, healthHeight);
    setImage(myImage);
}
Nooooob Nooooob

2017/4/7

#
Now it's not filled at all.
Nosson1459 Nosson1459

2017/4/7

#
I can't see the image (my problem), but what does it look like, transparent, white or red, because I used your fill code? If the variable 'health' is not equal to anything then it won't be red. If you're having problems and you still can't solve it then post the FULL class code.
Nooooob Nooooob

2017/4/7

#
Nosson1459 wrote...
post the FULL class code.
The code:
import greenfoot.*;
import java.awt.Color;
public class Healthbar extends X
{
    int health = 4;
    int healthWidth = 80;
    int healthHeight = 15;
    int pixelsPerHealthPoint = (int)healthWidth/health;
    public Healthbar()
    {
        update();
    }
    public void update()
    {
        setImage(new GreenfootImage(healthWidth + 2, healthHeight + 2));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.WHITE);
        myImage.drawRect(0, 0, healthWidth + 1, healthHeight + 1);
        myImage.setColor(Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthHeight);
    }
    public void loseHealth()
    {
        health--;
        update();
    }
}
and it looks simply like this:
Nosson1459 wrote...
I can't see the image (my problem)
I didn't put an image in the healthbar class. If X touches Y enough times, X will get removed but the bar doesn't decrease (at least I can't see it decrease).
Nosson1459 Nosson1459

2017/4/7

#
If that's the full class code then you didn't make the changes that I told you to, so how can you expect any difference if you didn't change anything?
Nooooob wrote...
and it looks simply like this:
Nosson1459 wrote...
I can't see the image (my problem)
I didn't put an image in the healthbar class.
I mean that I can't see your posted image "https://www2.pic-upload.de/img/32968228/rsf.png", all I see is a small little icon about 25 - 50 pixels big with a small painting of a hill and a cloud on it.
Nooooob Nooooob

2017/4/7

#
Nosson1459 wrote...
If that's the full class code then you didn't make the changes that I told you to, so how can you expect any difference if you didn't change anything?
I did switch the update method if you are referring to that but it didn't work. Before it was a red filled bar and with the change it became transparent instead of red. Here it is tho:
import greenfoot.*;
import java.awt.Color;
public class Healthbar extends X
{
    int health = 4;
    int healthWidth = 80;
    int healthHeight = 15;
    int pixelsPerHealthPoint = (int)healthWidth/health;
    public Healthbar()
    {
        update();
    }
    public void update() 
    {
            GreenfootImage myImage = new GreenfootImage(healthWidth + 2, healthHeight + 2);
            myImage.setColor(Color.WHITE);
            myImage.drawRect(0, 0, healthWidth + 1, healthHeight + 1);
            myImage.setColor(Color.RED);
            myImage.drawRect(1, 1, health * pixelsPerHealthPoint, healthHeight);
            setImage(myImage);
    }
    public void loseHealth()
    {
        health--;
        update();
    }
}
Nosson1459 Nosson1459

2017/4/7

#
Now I made a little mistake - 4 letters - on line 19 it should be fillRect instead of drawRect - I hadn't tested my code, and I wasn't taking my time when typing.
Nooooob Nooooob

2017/4/7

#
I see. Now it's red again but there is still no update.
Nosson1459 Nosson1459

2017/4/7

#
Show the X class and MyWorld class. I don't think the health bar should extend X, it should probably extend Actor. The loseHealth method works the problem is calling it through all the other classes.
Nooooob Nooooob

2017/4/7

#
X class
public class X extends Actor
{
    boolean touchingY = false;
    public void act()
    {
        Actor y = getOneIntersectingObject(Y.class);
        if(y != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            Healthbar healthbar = myworld.getHealthBar();
            if(touchingY == false)
            {
               healthbar.loseHealth();
               touchingY = true;
               if(healthbar.health <= 0)
               {
                   getWorld().removeObject(this);
               }
            }
        } 
        else 
        {
                touchingY = false;
        }
        
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX() - 1, getY());
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX() + 1, getY());
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() - 1);
        } 
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() + 1);
        }
    }
}
World
import greenfoot.*;
public class MyWorld extends World
{
   Healthbar healthbar = new Healthbar();
   public MyWorld()
   {    
       super(600, 400, 1); 
       addObject(new Healthbar(), 50, 10);
       addObject(new X(), 200, 100);
       addObject(new Y(), 400, 100);
   }
   public Healthbar getHealthBar()
   {
      return healthbar; 
   }
}
Nooooob Nooooob

2017/4/7

#
Nosson1459 wrote...
I don't think the health bar should extend X, it should probably extend Actor.
All right.
danpost danpost

2017/4/7

#
Nooooob wrote...
Nosson1459 wrote...
I don't think the health bar should extend X, it should probably extend Actor.
All right.
What was the purpose of the X class? Can you show its code please?
Nosson1459 Nosson1459

2017/4/7

#
Change lines 9 - 10 of the X class to:
MyWorld myworld = (MyWorld) getWorld();
danpost danpost

2017/4/7

#
danpost wrote...
Can you show its code please?
Sorry, I did not notice it above.
There are more replies on the next page.
1
2