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

2018/5/6

Making healthbar decrease if it get hit by bullet

OfficerRiot OfficerRiot

2018/5/6

#
I cant seem to figure this out here is my code for my health bar and the actor i want to take damage public void hitBullet() { Actor Bullet = getOneIntersectingObject(Bullet.class); if(Bullet != null) { World myWorld = getWorld(); Hoth hoth = (Hoth)myWorld; HealthBar healthbar = hoth.getHealthBar(); if(touchingBullet == false) { healthbar.loseHealth(); touchingBullet = true; if(healthbar.health <=0) { myWorld.removeObject(this); } } else { touchingBullet = false; } } } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class HealthBar here. * * @author (your name) * @version (a version number or a date) */ public class HealthBar extends Actor { int health = 35; 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--; } }
danpost danpost

2018/5/7

#
Did you set up the healthbar properly in your world? (show world class code too)
OfficerRiot OfficerRiot

2018/5/7

#
Yes sorry here's the code for the world-class import greenfoot.*; /** * Write a description of class MyWorld here. * @author (your name) @version (a version number or a date) */ public class Hoth extends World { /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/ private int gravity; HealthBar healthbar = new HealthBar(); /** * Constructor for objects of class Hoth. */ public Hoth() { super(700, 700, 1, false); Platform platform = new Platform(); addObject(platform, 300, 700); WalkerBoss walker = new WalkerBoss(); addObject(walker, 1000, 475); prepare(); } public HealthBar getHealthBar() { return healthbar; } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { HealthBar healthbar = new HealthBar(); addObject(healthbar,614,49); healthbar.setLocation(590,32); } }
danpost danpost

2018/5/7

#
Remove the first word in your prepare method.
OfficerRiot OfficerRiot

2018/5/7

#
danpost wrote...
Remove the first word in your prepare method.
Seems like that did not help at all the health bar does not change at all
danpost danpost

2018/5/7

#
OfficerRiot wrote...
Seems like that did not help at all the health bar does not change at all
Believe me, it helped, if only some. At least now, the health bar that is returned by getHealthBar will be the one in your world instead of a different one. Actually, you can totally remove that first line as you already had a HealthBar object assigned to healthbar (this removal will not produce any apparent change). Will look further into your issue.
OfficerRiot OfficerRiot

2018/5/7

#
Ok, thanks a lot your help is extremely appreciated. Getting into codding is a lot harder than expected :)
danpost danpost

2018/5/7

#
The following probably will not produce any apparent changes either. Since the health bar only needs to update when its value changes (other than initially), you can remove the act method from its class and add a call to update in the loseHealth method (after the value is changed).
danpost danpost

2018/5/7

#
Is hitBullet being called from the act method of the class?
OfficerRiot OfficerRiot

2018/5/7

#
danpost wrote...
Is hitBullet being called from the act method of the class?
AHA, what was it thanks! it works completely fine now :)
OfficerRiot OfficerRiot

2018/5/7

#
Ok I noticed that my health bar does not fill in completely ita like 3 quarters of the way filled in do you see anything in my code that might be causing that?
danpost danpost

2018/5/7

#
That is because your initial health is 35, the width of the bar is 100 and (int)35/100 = 2, your pixelsPerHealth value. Then total health, 35, times pixelsPerHealth, 2, is 70, the amount of the bar that gets filled. You cannot use a constant pixels per health value, but must calculate how much red should show each time it changes:
myImage.fillRect(1, 1, 100*health/35, healthBarHeight );
OfficerRiot OfficerRiot

2018/5/7

#
danpost wrote...
That is because your initial health is 35, the width of the bar is 100 and (int)35/100 = 2, your pixelsPerHealth value. Then total health, 35, times pixelsPerHealth, 2, is 70, the amount of the bar that gets filled. You cannot use a constant pixels per health value, but must calculate how much red should show each time it changes:
myImage.fillRect(1, 1, 100*health/35, healthBarHeight );
Ok that worked but now it seems like its to fast like i have my guy firing and nothing is shown for the health bar but after a while it just shoots all the way down why is this happening?
OfficerRiot OfficerRiot

2018/5/7

#
Ok i got it to kinda work but now it goes down way to fast i want it to take a sliver of the health not a small chunk
You need to login to post a reply.