This site requires JavaScript, please enable it in your browser!
Greenfoot back
ddemarco06
ddemarco06 wrote ...

2014/7/28

Creating a Health Bar for a Boss

ddemarco06 ddemarco06

2014/7/28

#
I want to thank all those who have been helping me the past few days, I couldn't have gotten this far without your advice. I feel like I am abusing this discussion board a bit but I've been struggling with this concept for a while now and although its a bigger question I thought I could at least put it out there. I am trying to create a health bar for my Boss actor. The Boss appears after you collect a certain number of objects, so the bar would need to appear at the same time. I watched some youtube videos and managed to get the bar at the top of the screen when the boss appears, but the bar doesn't decrease when you shoot the Boss. I don't know what I'm missing. Here is my code for the Boss Health bar so far: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class BossHealth extends Actor { int health = 4; int healthBarWidth = 80; int healthBarHeight = 15; int pixelsPerHealthPoint = (int)healthBarWidth/health; /** * Act - do whatever the BossHealth wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public BossHealth() { updateHealth(); } public void act() { updateHealth(); } public void updateHealth() { 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--; } } Per the video I watched, I inserted the following into my space world: BossHealth bosshealth = new BossHealth(); //At the top above the constructor public BossHealth getBossHealth() //This method I placed after the constructor. { return bosshealth; } In my Hero's class I have a method that adds the boss when the hero collects X number of objects, so I added the health bar here too: public void addBoss() { getWorld().removeObjects(getWorld().getObjects(Spacedog.class)); getWorld().removeObjects(getWorld().getObjects(Yarnball.class)); getWorld().addObject(new Boss(), 300, 300); getWorld().addObject(new BossHealth(), 322, 46); } Finally, in the Boss class I added a boolean at the top "boolean touchingLaser = false;" and I have hitBoss method which is called in the Laser class: public void hitBoss() { Space space = (Space) getWorld(); BossHealth bosshealth = space.getBossHealth(); if(touchingLaser == false) { bosshealth.loseHealth(); bosshealth.updateHealth(); touchingLaser = true; if (bosshealth.health <=0) { Greenfoot.playSound("Explosion.wav"); space.addObject(new Explosion(), getX(), getY()); Greenfoot.playSound("hooray.wav"); space.removeObjects(space.getObjects(Counter.class)); space.removeObject(this); space.addObject(new Congratulations(), space.getWidth()/2, space.getHeight()/2); } } else{ touchingLaser = false; } } Everything compiles without error, but it doesn't work. The health bar doesn't decrease when you shoot the Boss. Any help or advice would be appreciated. Thank you!
danpost danpost

2014/7/28

#
The bar is probably losing health points when 'loseHealth' is called; but, the image of the bar is not updated to show the new value from within the method.
danpost danpost

2014/7/28

#
Found another issue. the BossHealth instance you are adding into the world is not the one you reference in your Space world. Instead of creating a new one, you need to refer to the one already referenced in your Space world :
BossHealth bossHealth = ((Space)getWorld()).getBossHealth();
getWorld().addObject(bossHealth, 322, 46);
// or just
getWorld().addObject(((Space)getWorld()).getBossHealth(), 322, 46);
ddemarco06 ddemarco06

2014/7/28

#
Awesome! That did the trick! Didn't realize I was creating a new health bar vs. using the one I already referenced in the space world. It works now. Thanks again for all the help. You guys are the best!
You need to login to post a reply.