I am trying to have a health bar follow a bear as it is moving around the world but I am having difficulty achieving this.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* This class contains the scrolling actor. This actor will always be in the middle of the screen and the objects around him will be moved.
* You shouldn't change the code of this class because that will probably cause problems in the scrolling system.
*/
public class ScrollingActor extends Player
{
private boolean scrollingCenter;
public ScrollingActor() {
scrollingCenter = true;
}
public ScrollingActor(boolean scrollingCenter) {
this.scrollingCenter = scrollingCenter;
}
public final void addedToWorld(World world) {
if (scrollingCenter) {
List<ScrollingActor> scrollingActors = getWorld().getObjects(ScrollingActor.class);
if (scrollingActors != null && !scrollingActors.isEmpty()) {
for (ScrollingActor scrollingActor : scrollingActors) {
if (!scrollingActor.equals(this)) {
scrollingActor.setScrollingCenter(false);
}
}
}
}
}
/**
* An extended setLocation method.
* You should not change this method. That would cause problems in the programm.
*
* @param x
* The new x location of the actor.
*
* @param y
* The new x location of the actor.
*/
public final void setLocation(int x, int y) {
super.setLocation(x, y);
if (scrollingCenter) {
getWorld().resetPlayersPosition(this);
}
}
/**
* An extended setLocation method which uses double values for the coordinates.
* You should not change this method. That would cause problems in the programm.
*
* @param x
* The new x location of the actor.
*
* @param y
* The new x location of the actor.
*/
public final void setLocation(double x, double y) {
super.setLocation(x, y);
if (scrollingCenter) {
getWorld().resetPlayersPosition(this);
}
}
/**
* An extended setLocation method which uses double values for the coordinates and doesn't always reset the players position.
* You should not change this method. That would cause problems in the programm.
*
* @param x
* The new x location of the actor.
*
* @param y
* The new x location of the actor.
*
* @param resetPosition
* If you don't want to reset the players position this value has to be false.
*/
public final void setLocation(double x, double y, boolean resetPosition) {
super.setLocation(x, y);
if (scrollingCenter && resetPosition) {
getWorld().resetPlayersPosition(this);
}
}
public final boolean isScrollingCenter() {
return scrollingCenter;
}
public final void setScrollingCenter(boolean scrollingCenter) {
this.scrollingCenter = scrollingCenter;
}
}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(){
update();
}
public int getHealth()
{
return health;
}
public void act()
{
update();
}
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;
if(health <=0){
ExampleWorld exampleworld = new ExampleWorld();
GameOver gameover = new GameOver();
setImage(new GreenfootImage(exampleworld.getWidth()/2,exampleworld.getHeight()/2));
}
}
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;
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 (Greenfoot.isKeyDown("up")) {
setRotation(270);
move(3);
checkObstacle();
consume();
}
if (Greenfoot.isKeyDown("down")) {
setRotation(90);
move(3);
checkObstacle();
consume();
}
if (Greenfoot.isKeyDown("left")) {
setRotation(180);
move(3);
checkObstacle();
consume();
}
if (Greenfoot.isKeyDown("right")) {
setRotation(0);
move(3);
checkObstacle();
consume();
}
}
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){
GameOver gameover = new GameOver();
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);
}
}
}
