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
5
cloud41910 cloud41910

2014/11/29

#
the health bar works on my actor but the color is still green even though i got hit many times
danpost danpost

2014/11/29

#
cloud41910 wrote...
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?
I presume by the following post that this issue was resolved:
the health bar works on my actor but the color is still green even though i got hit many times
There are methods to change the value or percentage of the maximum value at which the color change happens. These are 'setBreakPercent' and 'setBreakValue'. The method used automatically determines which is used (value or percentage) for the color change. There are methods to change the colors used also -- 'setBreakColor' and 'setSafeColor'.
cloud41910 cloud41910

2014/11/30

#
How can i use those method it doesn't read the methods in the bar class and if i do it like this
     public void hit()  
    {  
        Actor heartlessblast = getOneIntersectingObject(HeartlessBlast.class);  
        if(heartlessblast != null)  
        {  
        Health = Health - 10;  
        getWorld().removeObject(heartlessblast);  
          World world = getWorld();
        
        Cl cl = (Cl)world;
        Bar bar = cl.getBar();
        bar.setBreakValue(brkVal);   
        }
I'll get an error
danpost danpost

2014/11/30

#
What error are you getting??? If it is 'cannot find variable brkVal', then it is because you did not declare the field 'brkVal' in the class. You would normally hard-code a value there -- maybe 20 or 25 (if maximumValue is 100).
cloud41910 cloud41910

2014/11/30

#
method setBreakValue in class Bar cannot be appplied to given types; required: int found:boolean reason:actual argument boolean cannot be converted to int by method invocation conversion That's the whole error i got
cloud41910 cloud41910

2014/11/30

#
in order to have access to the bar class's methods i did the same to my scoring counter class but i don't think it's applicable in the bar
danpost danpost

2014/11/30

#
The message is telling you that you are trying to pass a true or false value to the method when the method requires an int value. This means the bar methods are accessible, but not being used properly.
cloud41910 cloud41910

2014/11/30

#
The error is gone but still the color doesn't change
cloud41910 cloud41910

2014/11/30

#
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 int Health = 100;  
    public static int MaxHealth = 100;
    public int brkVal = 10;
    public void act()
    {
        movement();
        if(isOnGround()){
            CanJump = true;
        } else{
            CanJump = false;
            fall();
        }
        fire();
        shoot();
        healthBar.setLocation(getX(), getY()-30);  
        if (healthBar.getValue() == 0){
            getWorld().removeObject(healthBar);
            getWorld().removeObject(this);
        }
        hit();
    }
    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;
    }
    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);  
    }  
        public void hit()  
    {  
        Actor heartlessblast = getOneIntersectingObject(HeartlessBlast.class);  
        if(heartlessblast != null)  
        {  
        Health = Health - 10;  
        getWorld().removeObject(heartlessblast);  
          World world = getWorld();
        
        Cl cl = (Cl)world;
        Bar bar = cl.getBar();
        bar.setBreakValue(brkVal);   
        }  
        if(Health <= 0)  
        {  
            Actor bar = getOneIntersectingObject(Bar.class);  
            getWorld().removeObject(bar);  
            getWorld().removeObject(this);  
        }  
    } 
}
cloud41910 cloud41910

2014/11/30

#
that's my code following the blueprint so far
danpost danpost

2014/11/30

#
Ok. Some major issues here. First, you do not need to duplicate fields in the Bar class here in this class. Fields for 'Health', 'MaxHealth' and 'brkVal' are already defined in your 'healthBar' object. There values are accessible through 'healthBar.getValue', 'healthBar.getMaximumValue' and 'healthBar.getBreakValue'. Next, changing the value of the 'Health' field does nothing for the 'healthBar' object for the K object (see line 121). I see that you have 'brkVal' equal to '10', which is how much you are decreasing 'Health' by. I wonder if you are confused about what 'setBreakVal' does. Also, you are getting the Bar object through a method call to your Cl world object, which seems quite strange. The Bar object for the K object is referenced in the 'healthBar' field within the K class. And again on line 131, you are doing something else to get a reference to it when you already have one. Finally, you have code in your act method that is duplicated in the hit method. As is, you should be getting 'IllegalStateException' throws on line 118 when the value of the healthBar reaches zero (which may not be happening yet, as there is currently no code in the class to change the value of the healthBar. You can remove the following lines 22 through 24 and lines 37 through 40. If you want the healthBar to change colors when the health value goes below 10, add the following at line 49:
healthBar.setBreakValue(10);
You can remove lines 121 through 127 and replace them with the following line:
healthBar.subtract(10);
You can remove line 131 and change 'bar' in line 132 to 'healthBar'. That should clean it up a little bit. If problems persist, repost the class and copy/paste any error message in their entiretly and explain issue.
cloud41910 cloud41910

2014/11/30

#
What should i replace the Health variable with?
     if(Health <= 0)  
        {    
            getWorld().removeObject(healthBar);  
            getWorld().removeObject(this);  
        }
cloud41910 cloud41910

2014/11/30

#
since i removed lines 22-24
danpost danpost

2014/11/30

#
Change 'Health' to 'healthBar.getValue()' -- and the value should never be less than zero; so you can use '=='.
cloud41910 cloud41910

2014/11/30

#
It works now thanks! one major problem solved
There are more replies on the next page.
1
2
3
4
5