I'm trying to remove a health bar that is following a bear in my game. After a game over screen shows up, I wrote the code so that it removes the bear but I do not know how to remove the health bar along with the bear.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class HealthBar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class HealthBar extends ScrollingActor
{
/**
* Act - do whatever the HealthBar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
int health = 100;
int healthBarWidth = 100;
int healthBarHeight = 30;
int pixelsPerHealthPoint = (int) healthBarWidth/health;
public HealthBar(){
act();
}
public int getHealth()
{
return health;
}
public void act()
{
update();
if (Greenfoot.isKeyDown("up")) {
setRotation(270);
move(5);
}
if (Greenfoot.isKeyDown("down")) {
setRotation(90);
move(5);
}
if (Greenfoot.isKeyDown("left")) {
setRotation(180);
move(5);
}
if (Greenfoot.isKeyDown("right")) {
setRotation(0);
move(5);
}
}
public void update(){
setImage(new GreenfootImage(healthBarWidth+2, healthBarHeight +2));
GreenfootImage myImage = getImage();
myImage.setColor(Color.WHITE);
myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1);
myImage.setColor(Color.GREEN);
myImage.fillRect(1, 1, health * pixelsPerHealthPoint, healthBarHeight);
}
public void loseHealthRock(){
health-=5;
}
public void loseHealthPoisonousBerries(){
health-=7;
}
public void loseHealthBees(){
health-=15;
}
public void addHealthBerries(){
if(health <= 95){
health+=5;
}
if(health == 96){
health+=4;
}
if(health == 97){
health+=3;
}
if(health == 98){
health+=2;
}
if(health == 99){
health+=1;
}
}
public void addHealthFish(){
if(health <=93){
health+=7;
}
if(health == 94){
health+=6;
}
if(health == 95){
health+=5;
}
if(health == 96){
health+=4;
}
if(health == 97){
health+=3;
}
if(health == 98){
health+=2;
}
if(health == 99){
health+=1;
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This class is just an example. You can delete it or change the code.
* It's not necessary for the scrolling system.
*/
public class Bear extends ScrollingActor
{
boolean touchingRock = false;
boolean touchingFish = false;
GameOver gameover = new GameOver();
public Bear() {
GreenfootImage image = getImage();
image.scale(image.getWidth() *2, image.getHeight() *2);
setImage(image);
}
/**
* Here you can tell your actor what he has to do.
*/
public void act() {
if (getWorld() == null) return;
if (Greenfoot.isKeyDown("up"))
{
setRotation(270);
move(5);
consume();
checkObstacle();
}
if (getWorld() == null) return;
if (Greenfoot.isKeyDown("down")) {
setRotation(90);
move(5);
consume();
checkObstacle();
}
if (getWorld() == null) return;
if (Greenfoot.isKeyDown("left")) {
setRotation(180);
move(5);
consume();
checkObstacle();
}
if (getWorld() == null) return;
if (Greenfoot.isKeyDown("right")) {
setRotation(0);
move(5);
consume();
checkObstacle();
}
}
public void checkObstacle()
{ Actor rock = getOneIntersectingObject(Rock.class);
if(rock != null)
{
World myWorld = getWorld();
ExampleWorld exampleworld = (ExampleWorld)myWorld;
HealthBar healthbar = exampleworld.getHealthBar();
if(touchingRock == false){
healthbar.loseHealthRock();
touchingRock = true;
if(healthbar.health<=0){
myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
myWorld.removeObject(this);
}
}
}
else{
touchingRock = false;
}
}
public void consume()
{
Actor berry = getOneIntersectingObject(Berry.class);
Actor fish = getOneIntersectingObject(Fish.class);
if(berry!=null)
{
getWorld().removeObject(berry);
}
if(fish!=null)
{
getWorld().removeObject(fish);
World myWorld = getWorld();
ExampleWorld exampleworld = (ExampleWorld)myWorld;
HealthBar healthbar = exampleworld.getHealthBar();
if(touchingRock == false){
healthbar.addHealthFish();
touchingFish = true;
}
}
else{
touchingFish = false;
}
}
}
