how can i make my health bar decrease when the actor hit the edge of the world?
//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);
}
}
}
//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--;
}
}
//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);
}
}//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();
}
}
}