For my remake of Pacman I thought about adding a health bar to the game. I followed a youtube tutorial. My Greenfoot programme is saying there are no errors but it's not working. The health bar isn't going down when Pacman touches the BlauerGeist (blue ghost)
This is the code for the Pacman:
This is the code for the class HealthBar:
In the world class I defined the health bar and put in this code:
I hope somebody can help me with this problem, thank you.
public void hitBlauerGeist() { Actor BlauerGeist = getOneIntersectingObject(BlauerGeist.class); if (BlauerGeist!=null) { World myWorld = getWorld(); RoboterWelt roboterwelt = (RoboterWelt)myWorld; HealthBar healthbar = roboterwelt.getHealthBar(); if (touchingBlauerGeist == true) { healthbar.loseHealth(); touchingBlauerGeist = true; if(healthbar.health <=0) { myWorld.removeObject(this); } } } else { touchingBlauerGeist = false; }
public class HealthBar extends Actor { int health = 3; int healthBarWidth = 100; int healthBarHeight = 15; int pixelsPerHealthPoint = (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*pixelsPerHealthPoint, healthBarHeight); } public void loseHealth() { health --; } }
public HealthBar getHealthBar() { return healthbar; } private void prepare() { addObject(healthbar, 21, 32); }