When i intersect my character (User) with an enemy (EnemyNormal), my health bar isn't decreasing? I can't find where i've gone wrong, help would be much appreciated!
Healthbar class:
User:
World1:
public Healthbar getHealthBar()
{
return healthbar;
}
Also, if i am to get this working in world1, will it also work in other created world classes? What will I need to do to enable it to work? just add the getHealthbar subroutine?
public class Healthbar extends Actor
{
int health = 4;
int healthBarWidth = 80;
int healthBarHeight = 15;
int healthPointPixels = (int)healthBarWidth/health;
/**
* Act - do whatever the Healthbar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Healthbar()
{
update();
}
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.RED);
myImage.fillRect(1, 1, health*healthPointPixels, healthBarHeight);
}
public void loseHealth()
{
health--;
}
}public void hitEnemy()
Enemy:
move(4);
if (Greenfoot.getRandomNumber(100) < 10)
{
turn(Greenfoot.getRandomNumber(90) - 45);
}
if (getX() <= 100 || getX() >= getWorld().getWidth() - 100)
{
turn(180);
}
if (getY() <= 100 || getY() >= getWorld().getWidth() - 100)
{
turn(180);
}
{
Actor enemynormal = getOneIntersectingObject(EnemyNormal.class);
if(enemynormal != null)
{
World myWorld = getWorld();
World1 world1 = (World1)myWorld;
Healthbar healthbar = world1.getHealthBar();
if(touchingenemy == false)
{
healthbar.loseHealth();
touchingenemy = true;
if(healthbar.health <=0)
{
getWorld().removeObject(this);
}
}
} else {
touchingenemy = false;
}
}

