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

2019/5/1

TWO ACTORS DOESN'T SEEM TO TOUCH EACH OTHER BECAUSE HEALTH BAR DOESN'T DECREASE EVEN IF THEY TOUCH :(

gdrgnxxi gdrgnxxi

2019/5/1

#
//Code for my World where trash falls downwards to the chef

public class RealGame extends World
{
    HealthBar healthBar = new HealthBar();
    
    public RealGame()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    //its not void, bc healthbar is returning the healthbar to the realgame when it is called
    public HealthBar gethealthBar()
    {
        return healthBar;
    }
    
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
       
        Back back = new Back();
        addObject(back,75,40);

        Cheese cheese = new Cheese();
        addObject(cheese,404,62);
        
        Counter counter = new Counter();
        addObject(counter,75,114);
        HealthBar healthBar = new HealthBar();
        addObject(healthBar,639,50);
        healthBar.setLocation(706,39);
        healthBar.setLocation(702,36);;
        Chef chef = new Chef();
        addObject(chef,382,527);
        Trash trash = new Trash();
        addObject(trash,310,96);
    }
        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

#
//Code for Health Bar Class

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

       public HealthBar(){ //runs automatically when image created
        update();   
    }
    
    public void update(){
        setImage(new GreenfootImage(healthBarWidth + 2, healthBarHeight +2));
        GreenfootImage myImage = getImage();
        myImage.setColor(greenfoot.Color.WHITE);
        //coordinates of upper left and right (0,0)
        myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
        myImage.setColor(greenfoot.Color.GREEN);
        myImage.fillRect(1,1, health*pixelPerHealthPoint, healthBarHeight);
    }
    
        public void loseHealth(){
           health--;   
           update();
    }
}
gdrgnxxi gdrgnxxi

2019/5/1

#
//Code for the Actor chef which doesn't seem to touch the falling trash actor? (it's like they just pass thru each other :(( 

public class Chef extends Actor
{
    public Chef()
    {
        //resize chef
        GreenfootImage image = getImage();
        int myNewHeight = (int)image.getHeight() / 2 ;
        int myNewWidth = (int)image.getWidth() / 2;
        image.scale(myNewWidth, myNewHeight);
    }

    /**
     * Act - do whatever the Chef wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //walkIn(); //chef walks into screen
        
        if (Greenfoot.isKeyDown("RIGHT"))
        {
            move(5);
        }
        if (Greenfoot.isKeyDown("LEFT"))
        {
            move(-5);
        }
        if ("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
        hitTrash(); 
    }
    boolean touchingTrash = false;
    public void hitTrash()  
    {  
        Actor Trash = getOneIntersectingObject(Trash.class);
        if(Trash != null){
            World realWorld = getWorld(); 
            RealGame levelone= (RealGame)realWorld; //getting my realword object 
     // to the realgame type world, get everything it do
                HealthBar healthBar = levelone.gethealthBar();
                    if(touchingTrash == false){
                        healthBar.loseHealth();
                        touchingTrash = true;               
                    }
                }
            }
   
    public void fire()
    {
        Knife knife = new Knife();
        getWorld().addObject(knife, getX(), getY());
    }
}
danpost danpost

2019/5/1

#
Remove line 34 from your RealWorld class.
gdrgnxxi gdrgnxxi

2019/5/2

#
nothing still happens but when i put the hitTrash() method on the Health bar actor, the healthbar decreases once?
nccb nccb

2019/5/2

#
You are not resetting touchingTrash back to false when you are no longer touching the trash. So the health would only decrease once in the above code (after danpost's suggestion which clears up another issue). You need to add an else on the if (Trash != null) to set touchingTrash to false.
You need to login to post a reply.