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

4 days ago

Moving HP bar

andrewchin andrewchin

4 days ago

#
int num = 19 -hp;
                    
                    hpbar hpbar = (hpbar) getWorld().getObjects(hpbar.class).get(0);
        hpbar.setImage(num+".gif");
I have a HP bar where i change the image after each bit of damage is taken. However when the actor is in the process of moving (the HP bar moves under it), it shows the initial image. Any tips?
danpost danpost

4 days ago

#
andrewchin wrote...
<< Code Omitted >> I have a HP bar where i change the image after each bit of damage is taken. However when the actor is in the process of moving (the HP bar moves under it), it shows the initial image. Any tips?
You will probably have to show the entire class codes (at least, to start with).
andrewchin andrewchin

4 days ago

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;


public class protag extends Actor
{
    int powercount = 0;
    int ammo = 3;    
    int hp = 18;
    GifImage gifImage = new GifImage("char 1.gif");
    int speed = 3;

    public void destroyEnemies()
   {
       //"Enemy" can be any class that you want the bullet to destroy. 
       Actor enemy = getOneIntersectingObject(tree.class);
       if(enemy != null) {
            getWorld().removeObject(enemy);
            getWorld().removeObject(this);
       }
   }
    public void checkFire()
{
   if(Greenfoot.isKeyDown("space")) {
       ammo = ammo - 1;
       if(ammo <= 0){

           ammo = 3;
        }
   }
}
        public void controls()
    {
        int speed = 3;

  
        if(Greenfoot.isKeyDown("a")){
            setLocation(getX() - speed, getY());
            getWorld().removeObjects(getWorld().getObjects(hpbar.class));
            getWorld().addObject(new hpbar(), getX() - speed, getY() + 45);
        }
        if(Greenfoot.isKeyDown("d")){
            setLocation(getX() + speed, getY());
            getWorld().removeObjects(getWorld().getObjects(hpbar.class));
            getWorld().addObject(new hpbar(), getX()+speed, getY() + 45);

    }
    
    }
    public void act() 
    {   
        int num = 19 -hp;
                    
                    hpbar hpbar = (hpbar) getWorld().getObjects(hpbar.class).get(0);
        hpbar.setImage(num+".gif");
                
    
                    
                controls();
                setImage(gifImage.getCurrentImage());
                checkFire();
                destroyEnemies();
    
                if(hp<=0){
                    World W = getWorld();

                    W.removeObject(this);
                    W.removeObjects(W.getObjects(hpbar.class));

                    return;
                }
        if(isTouching(bomb.class)){
            hp = hp - 1;
            Actor bomb = getOneIntersectingObject(bomb.class);
            getWorld().removeObject(bomb);

            
        }
    }    
}
danpost danpost

4 days ago

#
I am not sure why you do not get an error on line 55. I would think it needs to be:
hpbar.setImage(""+num+".gif");
Out of curiosity, are your hpbar images animated? Just wondering because of the gif suffix. Please provide hpbar class codes.
You need to login to post a reply.