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

2014/11/28

How to apply HealthBar

1
2
3
4
cloud41910 cloud41910

2014/11/28

#
Guys i need some answers on how to apply the healthbar classes to my shooting game i've downloaded and viewed all scenarios but still can't apply it
danpost danpost

2014/11/28

#
The rest of this discussion deals with using my Progress bar/Health bar class. Maybe that will help.
cloud41910 cloud41910

2014/11/28

#
i already saw that but still can't figure out how to apply it
cloud41910 cloud41910

2014/11/28

#
also some other scenarios as well but still can't figure it out
danpost danpost

2014/11/28

#
With my class, just include it, as is, into your project and follow my blueprint for the Zombie class in the discussion thread linked above.
Max-Felix Max-Felix

2014/11/28

#
if you got a picture for your actor like this: image=new GreenfootImage("finish.png"); setImage(image); you can simply add a text to it this way: image.drawString("North",100,10); 100 and 10 are the coordinates and "North" a string, you add your health bar there i guess
cloud41910 cloud41910

2014/11/28

#
Now i've got an error could you please check my code here it is
cloud41910 cloud41910

2014/11/28

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

/**
 * Write a description of class K here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class K extends ShooterActors
{
    /**
     * Act - do whatever the K wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private int fspeed = 0;
    private int acceleration = 2;
    private int speed =5;
    boolean CanJump;
    KiBlast kiblast = new KiBlast();
    private int points = 0;
    private Bar healthBar;
    public void act()
    {
        movement();
        if(isOnGround()){
            CanJump = true;
        } else{
            CanJump = false;
            fall();
        }
        fire();
        shoot();
        
    }
    public K()  
    {  
        healthBar = new Bar("", "", 100, 100);  
        healthBar.setShowTextualUnits(false);  
        healthBar.setBarWidth(30);  
        healthBar.setBarHeight(3);  
    }  
    public boolean isOnGround()
    {
        Cl Window = (Cl) getWorld();
        if(getY() + (getImage().getHeight() / 2) >= (Window.getHeight() -1)) {
            return true;
        }
        return false;
        { healthBar.setLocation(getX(), getY()-30);  
        if (healthBar.getValue() == 0) getWorld().removeObect(this); } 
    }
    public void jump()
    {
        fspeed = -10;
        fall();
    }
    public void fall()
    {
        setLocation ( getX(), getY() + fspeed);
        fspeed = fspeed + acceleration;
    }
    public void movement()
    {      
    if(Greenfoot.isKeyDown("left"))
    {
        
        MoveLeft();
    }    
    if(Greenfoot.isKeyDown("right"))
    {
        
        MoveRight();
    }
    if(Greenfoot.isKeyDown("up"))
    {
     jump();
    }
    }
    public void MoveLeft()
    {
        setLocation (getX() - speed, getY());
    }
    public void MoveRight()
    {   
        setLocation (getX() + speed, getY());
    }
    public void fire()
    {
    if(Greenfoot.isKeyDown("x"))
    {
        World world = getWorld();
        world.addObject(kiblast, 0, 0);
        kiblast.setLocation(getX(),getY());
    }
    }
    public void shoot()
    {
         
        {  
         if ("c".equals(Greenfoot.getKey()))  
          
        getWorld().addObject(new KiBlast(), getX(), getY());  
          points = points+5;
       }  
    }
     protected void addedToWorld(World world)  
    {  
        world.addObject(healthBar, getX(), getY()-30);  
    }  
}
 
cloud41910 cloud41910

2014/11/28

#
my error is on line 38
cloud41910 cloud41910

2014/11/28

#
I fixed it but my only problem is that how will my hp be depleted when i get shot??
danpost danpost

2014/11/28

#
When the actor detects it has been hit and damage is done, use the 'subtract' method on the healthbar object to reduce its value.
cloud41910 cloud41910

2014/11/28

#
how would my actor detect it? can i have the code for it?please
cloud41910 cloud41910

2014/11/28

#
i understand what you mean but i can't apply it to my actor
danpost danpost

2014/11/29

#
Use one of the Actor class methods that are used for collision (intersection). Basically, if intersects damaging object, remove damaging object and decrement healthbar value; if healthbar value is zero, remove the actor.
cloud41910 cloud41910

2014/11/29

#
how can i make the method of the bar available to my Main character actor also i'm going to use intersecting object for this right?
There are more replies on the next page.
1
2
3
4