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

2014/3/17

Health Bar Help

1
2
hugtvy hugtvy

2014/3/17

#
I know this topic has been made a lot however I cannot open scenarios on my boarding school's due to java on the internet being blocked therefore i cannot actually view the scenario on HpBars. Is there anyone someone could show me how to make one without the scenario? my code is pretty simple, usual private int for health and then health = X-20
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Survivor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Survivor extends Actor
{
    
    private int counter = 0;
    private int counter1 = 0;
    private int health = 60;
    
    /**
     * Act - do whatever the Survivor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    

    public void act() 
    {
        shoot();
        move();
        
        Helper();
        Healer();
        shooter();
        
        HealthKit();
        HealthKit1();
        ZombieHit();
        ZombieHit1();
        ZombieHit2();
        SurvivorZombieHit();
        SurvivorZombieHit1();
        SurvivorZombieHit2();
        
        MouseInfo m = Greenfoot.getMouseInfo();  
            if(m != null)
            turnTowards(m.getX(), m.getY());
       
             
    }
    
            
            
        public void HealthKit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, HealthKit.class);
        if (zombie != null && (health  == 40))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 40 + 20;
        }
    }
    
    
    
    public void HealthKit1()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, HealthKit.class);
        if (zombie != null && (health  == 20))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 20 + 20;
        }
    }
    
    
    
    public void ZombieHit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null && (health == 60))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 60 - 20;
        }
    }
    
    
    
    public void ZombieHit1()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null && (health == 40))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 40 - 20;
        }
    }
    
    
    
    public void ZombieHit2()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, Zombie.class);
        if (zombie != null && (health == 20))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 20 - 20;
            if (health == 0)
            {
                getWorld().removeObject(this);
                Counter counter;
                counter = (Counter)world.getObjects(Counter.class).get(0);
                GreenfootImage image = new GreenfootImage("FINAL SCORE\n"+counter.getValue(), 84, java.awt.Color.green, null);
                counter.setImage(image);
                counter.setLocation(world.getWidth()/2, world.getHeight()/2);
                Greenfoot.stop();
              
            }
        }
    }
   
    
    
      public void SurvivorZombieHit()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, SurvivorZombie.class);
        if (zombie != null && (health == 60))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 60 - 20;
        }
    }
    
    
    
    public void SurvivorZombieHit1()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, SurvivorZombie.class);
        if (zombie != null && (health == 40))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 40 - 20;
        }
    }
    
    
    
    public void SurvivorZombieHit2()
    {
        Actor zombie;
        zombie = getOneObjectAtOffset(0, 0, SurvivorZombie.class);
        if (zombie != null && (health == 20))
        {   
            World world;
            world = getWorld();
            world.removeObject(zombie);
            health = 20 - 20;
            if (health == 0)
            {
                getWorld().removeObject(this);
                Counter counter;
                counter = (Counter)world.getObjects(Counter.class).get(0);
                GreenfootImage image = new GreenfootImage("FINAL SCORE\n"+counter.getValue(), 84, java.awt.Color.green, null);
                counter.setImage(image);
                counter.setLocation(world.getWidth()/2, world.getHeight()/2);
                Greenfoot.stop();
              
            }
        }
    }
}      
Any help is appricated!
danpost danpost

2014/3/17

#
There seems to be a lot of code here that can be cleaned up; especially with regard to the 'Hit' and 'Kit' methods. The following can replace lines 50 through 77 above:
private void HealthKit()
{
    Actor kit = getOneObjectAtOffset(0, 0, HealthKit.class);
    if (kit == null) return;
    adjustHealth(20);
    getWorld().removeObject(kit);
}
// adding this method to adjust and limit the values allowed for 'health'
private void adjustHealth(int adjustment)
{
    health += adjustment; // or 'health = health+adjustment;'
    if (health > 60) health = 60;
    if (health < 0) health = 0;
}
Lines 80 through 133 can be replaced with this (the check for no health will be done later):
private void ZombieHit()
{
    Actor zombie = getOneObjectAtOffset(0, 0, Zombie.class);
    if (zombie==null) return;
    adjustHealth(-20);
    getWorld().removeObject(zombie);
}
and finally, lines136 through 188 can be replaced with this:
private void SurvivorZombieHit()
{
    Actor zombie = getOneObjectAtOffset(0, 0, SurvivorZombie.class);
    if (zombie == null) return;
    adjustHealth(-20);
    getWorld().removeObject(zombie);
}
Now, the main reason I removed the check to see if 'health' was zero from 'ZombieHit2' and 'SurvivorZombieHit2' was because (1) because you really only need to check this once after all damage has been assessed and (2) to avoid some possible exceptions (NullPointer and IllegalState) when any one of them removes the Survivor object from the world. We can put the health check code in a method by itself private void checkHealth() { if (health > 0) return; World world = getWorld(); world.removeObject(this); Counter counter = (Counter)world.getObjects(Counter.class).get(0); GreenfootImage image = new GreenfootImage("FINAL SCORE\n"+counter.getValue(), 84, java.awt.Color.green, null); counter.setImage(image); counter.setLocation(world.getWidth()/2, world.getHeight()/2); Greenfoot.stop(); } Finally, we can re-write your act method to accommodate these changes:
public void act() 
{
    MouseInfo m = Greenfoot.getMouseInfo();  
    if (m != null) turnTowards(m.getX(), m.getY());
    shoot();
    move();
    Helper();
    Healer();
    shooter();
    HealthKit();
    ZombieHit();
    SurvivorZombieHit();
    checkHealth();
}
It made sense to have your actor face the mouse before doing some of the other actions instead of after. As far as I know, that is the extent of help I can give with what was given (I do not know what some of the other methods called from 'act' do; so, I cannot give any advice on them or how they affect the execution of the act method). I can give one more suggestion: if your SurvivorZombie class extends your Zombie class, then you can safely remove the 'SurvivorZombieHit' method and its call from the 'act' method.
hugtvy hugtvy

2014/3/18

#
wow cheers, will get on that right away, however you havent told me how i can make a health bar haha, sorry to be persistant. Also survivorZombie is a subclass of helper so it wouldnt work.
danpost danpost

2014/3/18

#
hugtvy wrote...
wow cheers, will get on that right away, however you havent told me how i can make a health bar haha, sorry to be persistant. Also survivorZombie is a subclass of helper so it wouldnt work.
You have not shown anything as far as a healthbar class, yet. Anyway, getting what you already have working is a pre-requisite to moving on. You should compile and test often to eliminate any problems as soon as possible. Pushing on without doing so could escalate the problems and make them harder to fix.
hugtvy hugtvy

2014/3/18

#
alright ive changed it, yer there are no longer any errors however, i can now always pick up the healthkit, which i dont want to be able to do. i should only be able to pick up healthkits if i have a lower health then 60? should i just make it (kit == null) && (counter <60) return?
danpost danpost

2014/3/18

#
Just add as first line of 'HealthKit' method:
if (health == 60) return;
hugtvy hugtvy

2014/3/18

#
alright cheers, i seemed to have fixed everything, if you want to see the game its available here
danpost danpost

2014/3/18

#
Looks to be a pretty good start. :+)
hugtvy hugtvy

2014/3/18

#
its pretty bad i know haha, alright so how do i make this health bar, i have one idea but its super complicated, basically, if health = 60 i getWorld().addObject a pic of a full healthbar ill make on photoshop, then if i get hit, and my health goes down to 40 ill make it so that it adds a pic that looks 66.66% full, then if it goes to 20 ill add another that looks 1/3 full that will be red instead of green and maybe add an annoying sound indication or something. but thats probs not right haha
danpost danpost

2014/3/18

#
I would make the image of the healthbar programmatically using the GreenfootImage class constructors and methods.
hugtvy hugtvy

2014/3/18

#
alright how do i do that haha?
danpost danpost

2014/3/18

#
Probably the best way to start is to experiment. Use one of the constructor to create an image and then use the different methods to alter what that image displays. See what does what. Then, with that experience, work on the healthbar. If you have problems at that point, start a discussion thread -- show code, explain problem, explain what you want, how it is not developing like you want, etc.
hugtvy hugtvy

2014/3/19

#
whats a constructor? haha
danpost danpost

2014/3/19

#
You should look at my scenario Overview: Subclassing in Greenfoot. Then, check the Java tutorials out.
hugtvy hugtvy

2014/3/19

#
and why am i doing this again? cant i just import an image?
There are more replies on the next page.
1
2