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

2019/4/26

HEALTH BAR UPATE

gdrgnxxi gdrgnxxi

2019/4/26

#
how can i make my health bar decrease when the actor hit the edge of the world?
danpost danpost

2019/4/26

#
Like Johnny-5 would say "Need input." Health bar, actor and world class codes, please.
gdrgnxxi gdrgnxxi

2019/5/1

#
//THIS IS THE CODE FOR THE WORLD of the game. (when a trashcan drops, the chef must shoot it. Once the trashcan touches the edge, health bar must decrease

public class RealGame extends World
{
    public RealGame()
    {    
        // 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()
    {
        Chef chef = new Chef();
        addObject(chef,516,337);
        chef.setLocation(353,494);
        chef.setLocation(401,485);
        chef.setLocation(395,496);
        chef.setLocation(398,476);

        Back back = new Back();
        addObject(back,75,40);

        Cheese cheese = new Cheese();
        addObject(cheese,404,62);

        Trash trash = new Trash();
        addObject(trash,347,128);
        trash.setLocation(315,56);
        Counter counter = new Counter();
        addObject(counter,75,114);
        HealthBar healthBar = new HealthBar();
        addObject(healthBar,639,50);
        healthBar.setLocation(706,39);
        healthBar.setLocation(702,36);
    }
        int iCounter = 0;
    
    public void act()
    {
        iCounter++;
        if(iCounter == 60)
        {
            iCounter = 0;

            Trash myTrash = new Trash();
            addObject(new Trash(), Greenfoot.getRandomNumber(getWidth()),0);
        }
    }
}

gdrgnxxi gdrgnxxi

2019/5/1

#
//THIS IS THE CODE FOR HEALTH BAR. 

public class HealthBar extends Actor
{
    int health = 3;
    int healthBarWidth = 150;
    int healthBarHeight = 15;
    int pixelPerHealthPoint = (int)healthBarWidth/health;
    
    public HealthBar(){
        update();   
    }

    public void act() 
    {
        update();
    }
    
    public void update(){
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight +2));
        GreenfootImage myImage = getImage();
        myImage.setColor(greenfoot.Color.WHITE);
        myImage.drawRect(0, 0, healthBarWidth + 2, healthBarHeight + 2);
        myImage.setColor(greenfoot.Color.GREEN);
        myImage.fillRect(1,1, health*pixelPerHealthPoint, healthBarHeight);
    }

        public void loseHealth(){
     health--;   
    }
}
gdrgnxxi gdrgnxxi

2019/5/1

#
//THIS IS THE CODE FOR THE ACTOR-TRASH CAN. (i want the health bar to decrease when the dropping trashcan hit the edge of the world)

public class Trash extends Actor
{
    public Trash()
    {
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 20;
        int myNewWidth = (int)image.getWidth() / 20;
        image.scale(myNewWidth, myNewHeight);
    }
    
    public void act() 
    {
        fall2();
        getHit();
    }    
     public void fall2() 
    {
        setLocation(getX(), getY() + 2);
    }
    
     public void getHit()
    {
        Actor Knife = getOneObjectAtOffset(0,0, Knife.class);
        if (Knife != null) getWorld().removeObject(this);
       
    }
}
gdrgnxxi gdrgnxxi

2019/5/1

#
//I PUT THIS CODE IN ACTOR-TRASH CLASS. 

public class Trash extends Actor
{
    HealthBar health = new HealthBar(); //to call loseHealth method on HealthBar class
    public Trash()
    {
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 20;
        int myNewWidth = (int)image.getWidth() / 20;
        image.scale(myNewWidth, myNewHeight);
    }
  
    public void act() 
    {
        fall2();
        getHit();
        atEdge();
    }
       
    public void fall2() //HINDI SMOOTH
    {
        setLocation(getX(), getY() + 2);
    }
    
     public void getHit()
    {
        Actor Knife = getOneObjectAtOffset(0,0, Knife.class);
        if (Knife != null) getWorld().removeObject(this);
       
    }
    public void atEdge()    //for losing health when actor trash is at the edge of the world
    {   
        int x = getX()+5;
        int y = getY()-5;
        
       if (getX() == 298 && getY() == 599){ 
        health.loseHealth();
    }
}
}
danpost danpost

2019/5/1

#
In your atEdge method, your conjunction is wrong. Using '&&', you are restricting the check to the corner where the two edges meet. Also, you should probably be using '>=" as the actor could pass right over either of the exact given coordinate (although, I do not see why you check along the x-axis at the right edge since the actor only moves straight down).
You need to login to post a reply.