Hey guys, could use your help if you don't mind. I've been banging my head against this code for 3 hours, trying to get the healthBar to update correctly. I have a scenario with up to 6 worlds. I need the healthBar to update and carry over to the other worlds. It was working okay with just one world, prior to doing creating the abstract world.
I'll post the code only pertaining to the HealthBar:
AbstractWorld:
All Area Worlds have a getter and method to add the bar:
HealthBar code:
Method call from enemy actor that uses the loseHealth method:
Any help most appreciated!
public void prepare(){
setPlayer();
healthBar();
setEnemies();
setWalls();
setExit();
showScore();
}
public abstract HealthBar getHealthBar();
public abstract void healthBar();
public HealthBar getHealthBar(){
return healthBar;
}
public void healthBar(){
HealthBar healthBar = new HealthBar();
addObject(healthBar, 800, 25);
showText("HEALTH", 800, 10);
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.Object;
public class HealthBar extends Actor
{
//If you want to change the amount of health the player has, do it here
int health = 10;
int barWidth = 160;
int barHeight = 15;
int reduceHealth = (int)barWidth/health;
public HealthBar()
{
drawBar();
}
public void act()
{
drawBar();
}
private void drawBar()
{
//Create an empty Greenfoot Image and assign it to a variable
setImage (new GreenfootImage (barWidth, barHeight));
GreenfootImage myImage = getImage();
//Draw the border and fill it with a color
myImage.setColor(Color.WHITE);
myImage.drawRect(0, 0, barWidth + 1, barHeight + 1);
myImage.setColor(Color.RED);
myImage.fillRect(1, 1, health*reduceHealth, barHeight);
}
/*
* Recommend setting up formal parameter for damage to accept actual paramters of damage
* from different types of monsters.
*/
public void loseHealth()
{
//Method for actor to call to lose health
health --;
}
public void gainHealth()
{
health++;
}
}
public void damagePlayer(){
Actor Player = getOneIntersectingObject(Player.class);
AbstractWorld aWorld = (AbstractWorld)getWorld();
HealthBar healthBar = aWorld.getHealthBar();
if (Player != null){
if (touchingPlayer == false)
{
healthBar.loseHealth();
touchingPlayer = true;
}
}
else{
touchingPlayer = false;
}
}
