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

2019/1/11

Need help updating the Health Bar.

EpicBaller5621 EpicBaller5621

2019/1/11

#
Hey guys, I need help updating my hp bar. I already have the health subclass for actor, but I want to implement it to where if myCharacter actor touches the enemy1 actor, the bar updates and decreases a life. This is the only code I have for the health.
public class Health extends Actor
{
    GreenfootImage[] hearts = { new GreenfootImage("0hearts.png"),
                            new GreenfootImage("1hearts.png"),
                            new GreenfootImage("2hearts.png"),
                            new GreenfootImage("3hearts.png") };
      int health = 3;
 
    private void updateImage()
    {
         setImage(hearts[health]);
    }
    /**
     * Act - do whatever the Health wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    } 
    public Health()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 25, image.getHeight() - 25);
        setImage(image);
        updateImage();
    }
    public void adjustHearts(int adjustmentValue)
    {
        health += adjustmentValue;
    }
    
}
danpost danpost

2019/1/11

#
You probably only need to call updateImage after adjusting the value in adjustHearts.
EpicBaller5621 EpicBaller5621

2019/1/12

#
danpost wrote...
You probably only need to call updateImage after adjusting the value in adjustHearts.
I called updateImage, but it's not updating when I am touching an enemy. Do I need to create a boolean in myCharacter and enemy1?
danpost danpost

2019/1/12

#
EpicBaller5621 wrote...
I called updateImage, but it's not updating when I am touching an enemy. Do I need to create a boolean in myCharacter and enemy1?
The problem is elsewhere then. Maybe in your World subclass or maybe in your player class.
EpicBaller5621 EpicBaller5621

2019/1/12

#
This is my character class. I think it has to be in my character class, but I can't seem to spot it?
public class myCharacter extends Actor
{
    //gravity
    private final int gravity = 1;
    private int velocity;
    //animation
    private GreenfootImage run1 = new GreenfootImage("running1.png");
    private GreenfootImage run2 = new GreenfootImage("running2.png");
    private GreenfootImage run3 = new GreenfootImage("running3.png");
    private GreenfootImage run4 = new GreenfootImage("running4.png");
    private GreenfootImage run5 = new GreenfootImage("running5.png");
    private GreenfootImage run6 = new GreenfootImage("running6.png");
    private GreenfootImage run7 = new GreenfootImage("running7.png");
    private GreenfootImage run8 = new GreenfootImage("running8.png");
    private GreenfootImage left1 = new GreenfootImage("left1.png");
    private GreenfootImage left2 = new GreenfootImage("left2.png");
    private GreenfootImage left3 = new GreenfootImage("left3.png");
    private GreenfootImage left4 = new GreenfootImage("left4.png");
    private GreenfootImage left5 = new GreenfootImage("left5.png");
    private GreenfootImage left6 = new GreenfootImage("left6.png");
    private GreenfootImage left7 = new GreenfootImage("left7.png");
    private GreenfootImage left8 = new GreenfootImage("left8.png");
    //shooting
    public int facing = 0;
    private int ShootingCounter = 35;
    //speed / speedofAnimation
    private int frame = 1;
    private int frame2 = 1;
    private int animationCounter = 0;
    private int speed = 3;
    


    public myCharacter()
    {
        GreenfootImage image = getImage();
        image.scale(95, 95);
        setImage(image);
        //gravity
        velocity = 1;
    }
    public void fall() //gravity pulling character
    {
        setLocation(getX(), getY() + velocity);
        if(SG())
        {
            velocity = 0;
            while(SG())
            {
                setLocation(getX(),getY() - 1);
            }
            setLocation(getX(), getY() + 1);

        }
        else if (velocity < 0 && headCollision()) velocity = 0;
        else velocity += gravity;

    }
    public void Jump()
    {
        velocity = -15;
    }
    public void CharResize()
    {
        GreenfootImage image = getImage();
        image.scale(95, 95);
        setImage(image);
    }
    /**
     * Act - do whatever the myCharacter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       moveMyCharacter();
       animationCounter++;
       fall();
       if(Greenfoot.isKeyDown("space")&& SG())Jump();
       CharResize();
       ShootingCounter --;
       shooting(); 
       shooting2();
       
       





    }
    public int getFacing()
    {
        int facing = 0;
        return facing;
    }
    public boolean SG() //solid ground
    {
        boolean SG = false;

        if(getY() > getWorld().getHeight() - 50) SG = true;
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth / -3, imageHeight / 3,myPlatform.class)!= null ||
            getOneObjectAtOffset(imageWidth / 3, imageHeight / 3,myPlatform.class )!= null)
            SG = true;
        if (getOneObjectAtOffset(imageWidth / -4, imageHeight / 4,platform2.class)!= null ||
            getOneObjectAtOffset(imageWidth / 4, imageHeight / 4,platform2.class )!= null)
            SG = true; 
        return SG;
    }
    public void moveRight()
    {
         setLocation(getX()+speed, getY());
         if(animationCounter % 4 == 0)
         {
          animateRR();
         }


    }
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
        if(animationCounter % 4 == 0)
         {
          animateLR();
         }

    }
    public void moveMyCharacter()
    {
        int y = getY();
        int x = getX();


         if(Greenfoot.isKeyDown("left")) // moves left
         {
           moveLeft();
           facing = -1;
           getFacing();
         }

        if(Greenfoot.isKeyDown("right")) // moves right
        {
           moveRight();
           facing = 1;
           getFacing();
        }


    }
    public boolean shooting()
    {
        if(Greenfoot.isKeyDown("x") && ShootingCounter <= 0 && facing == 1)
        {
            getWorld().addObject(new ShootRight(), getX(), getY());
            ShootingCounter = 100;
            return true;
        }
        
        return false;
    }
    public  boolean shooting2()
    {
        if(Greenfoot.isKeyDown("c") && ShootingCounter <= 0 && facing == -1)
        {
            getWorld().addObject(new ShootLeft(), getX(), getY());
            
            ShootingCounter = 100;
            return true;
        }
        return false;
    }
    
    public void animateRR()//running right animation
    {
        if(frame == 1)
        {
            setImage(run1);
        }
        else if(frame == 2)
        {
            setImage(run2);
        }
        else if(frame == 3)
        {
            setImage(run3);
        }
        else if(frame == 4)
        {
            setImage(run4);
        }
        else if(frame == 5)
        {
            setImage(run5);
        }
        else if(frame == 6)
        {
            setImage(run6);
        }
        else if(frame == 7)
        {
            setImage(run7);
        }
        else if(frame == 8)
        {
            setImage(run8);
            frame = 0;
            return;
        }

        frame ++;
    }

    public void animateLR()//running left animation
    {
        if(frame2 == 1)
        {
            setImage(left1);
        }
        else if(frame2 == 2)
        {
            setImage(left2);
        }
        else if(frame2 == 3)
        {
            setImage(left3);
        }
        else if(frame2 == 4)
        {
            setImage(left4);
        }
        else if(frame2 == 5)
        {
            setImage(left5);
        }
        else if(frame2 == 6)
        {
            setImage(left6);
        }
        else if(frame2 == 7)
        {
            setImage(left7);
        }
        else if(frame2 == 8)
        {
            setImage(left8);
            frame2 = 0;
            return;
        }

        frame2 ++;
    }
    public boolean headCollision() // checks if head is touching platform.
    {
        boolean headBumped = false;

        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if(getOneObjectAtOffset(imageWidth / -4, imageHeight / -4, myPlatform.class) !=null ||
            getOneObjectAtOffset(imageWidth / 4, imageHeight / -4, myPlatform.class) !=null)
            headBumped = true;
        if(getOneObjectAtOffset(imageWidth / -4, imageHeight / -4, platform2.class) !=null ||
            getOneObjectAtOffset(imageWidth / 4, imageHeight / -4, platform2.class) !=null)
            headBumped = true;    
            return headBumped;
    }
    
   
    
   
}
and this is my world sub class
public class myWorld extends World
{
    
    /**
     * Constructor for objects of class myWorld.
     * 
     */
    public myWorld() //constructor
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
        
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        myCharacter myCharacter = new myCharacter();
        addObject(myCharacter,115,282);
        myPlatform platform = new myPlatform();
        addObject(platform, 275, 475);
        myPlatform platform2 = new myPlatform();
        addObject(platform2, 475, 475);
        myCharacter.setLocation(59,290);
        myCharacter.setLocation(34,321);
        myCharacter.setLocation(47,293);
        myCharacter.setLocation(38,340);
        myCharacter.setLocation(34,249);
        myCharacter.setLocation(34,292);
        enemy1 enemy1 = new enemy1();
        addObject(enemy1,790,258);
        enemy1.setLocation(792,266);
        enemy1.setLocation(792,267);
        enemy1.setLocation(584,123);
        platform2 platformgg = new platform2();
        addObject(platformgg, 489, 245);
        Health hearts = new Health();
        addObject(hearts, 34,7);
        enemy1.setLocation(473,93);

        
        
    }
    
    
}
danpost danpost

2019/1/12

#
I do not see where you call the adjustHearts method from. Where is the code for when health is lost?
EpicBaller5621 EpicBaller5621

2019/1/12

#
Ok, so I changed my code around, and came with a new Health code,
public class HealthBar extends Actor
{
    int health = 4;
    int healthBarWidth = 80;
    int healthBarHeight = 17;
    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 update()
    {
        setImage(new GreenfootImage(healthBarWidth + 3 , healthBarHeight + 3));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.BLUE);
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(Color.RED);
        myImage.fillRect(1, 1, health*pixelsPerHealthPoint, healthBarHeight);
    }
    public void loseHealth()
    {
        health--;
        
    }
}
and then I created a killPlayer in myCharacter
public void killPlayer()
    {
        if(isTouching(enemy1.class))
        {
            World TheWorld = getWorld();
            myWorld MYWorld = (myWorld)TheWorld;
            HealthBar healthbar = MYWorld.getHealth();
            if(isTouching(enemy1.class) == true )
            {
                healthbar.loseHealth();
                touchingCharacter = true;
            }
        }
    }
but it still would not work.
danpost danpost

2019/1/12

#
EpicBaller5621 wrote...
Ok, so I changed my code around, and came with a new Health code, << Code Omitted >> and then I created a killPlayer in myCharacter << Code Omitted >> but it still would not work.
Do you now call killPlayer from the act method?
EpicBaller5621 EpicBaller5621

2019/1/13

#
danpost wrote...
EpicBaller5621 wrote...
Ok, so I changed my code around, and came with a new Health code, << Code Omitted >> and then I created a killPlayer in myCharacter << Code Omitted >> but it still would not work.
Do you now call killPlayer from the act method?
Yes, and the health bar does not decrease.
danpost danpost

2019/1/13

#
Maybe you should show your revised myWorld class codes. It has obviously been changed since the last posting of it (you call getHealth which is not shown above).
EpicBaller5621 EpicBaller5621

2019/1/13

#
danpost wrote...
Maybe you should show your revised myWorld class codes. It has obviously been changed since the last posting of it (you call getHealth which is not shown above).
I called the healthbar class
public class myWorld extends World
{
    HealthBar healthbar = new HealthBar();
    /**
     * Constructor for objects of class myWorld.
     * 
     */
    public myWorld() //constructor
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
        
    }
    public HealthBar getHealth()
    {
        return healthbar;
    }
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        myCharacter myCharacter = new myCharacter();
        addObject(myCharacter,115,282);
        myPlatform platform = new myPlatform();
        addObject(platform, 275, 475);
        myPlatform platform2 = new myPlatform();
        addObject(platform2, 475, 475);
        myCharacter.setLocation(59,290);
        myCharacter.setLocation(34,321);
        myCharacter.setLocation(47,293);
        myCharacter.setLocation(38,340);
        myCharacter.setLocation(34,249);
        myCharacter.setLocation(34,292);
        enemy1 enemy1 = new enemy1();
        addObject(enemy1,790,258);
        enemy1.setLocation(792,266);
        enemy1.setLocation(792,267);
        enemy1.setLocation(584,123);
        platform2 platformgg = new platform2();
        addObject(platformgg, 489, 245);
        
        
        enemy1.setLocation(473,93);

        addObject(healthbar, 100, 25);
        
    }
    
    
}
danpost danpost

2019/1/13

#
Okay, your HealthBar class code is the culprit. The issue is that you are not updating the image when the value changes. You may also find that using pixelsPerHealthPoint will not work as you might think (although, in your case, it might be alright).
EpicBaller5621 EpicBaller5621

2019/1/13

#
Yeah, the HealthBar was the culprit. Is there a way to add a counter to make it take away 1 life at a time instead of the health-depleting all the way to zero as soon as it touches the enemy?
danpost danpost

2019/1/13

#
EpicBaller5621 wrote...
Yeah, the HealthBar was the culprit. Is there a way to add a counter to make it take away 1 life at a time instead of the health-depleting all the way to zero as soon as it touches the enemy?
Try this:
public void killPlayer()
{
    if (isTouching(enemy1.class) != touchingCharacter)
    {
        touchingCharacter = !touchingCharacter;
        if (touchingCharacter)
        {
            ((myWorld)getWorld()).getHealth().loseHealth();
        }
    }
}
You need to login to post a reply.