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

2011/5/22

Health Bar

roccoricciardi roccoricciardi

2011/5/22

#
I'd like to initiate a counter for my classes that would depreciate as my actors are hit by bullets. How would i go about keeping track of the damage and changing the image of my health bar simultaneously? I'm getting confused because my actor is the one who "sees" the bullet, so how do i send that data to my health bar class so that I can change it accordingly??
davmac davmac

2011/5/23

#
If there is only one actor/health bar, then you can use the technique described here. If you have multiple actors, each with their own health bar, you can use a variation on that technique. Each actor will need to maintain a reference to the health bar:
    private HealthBar healthBar;
You will need to initialise the field so that it points to an appropriate health bar. An easy way to do this is have the actor create the health bar in the actor's constructor:
    healthBar = new HealthBar();
    getWorld().addObject(healthBar, getX(), getY() - 15); // may need to alter position
Then you will be able to call methods on the health bar from the actor:
    healthBar.decrease(); // assuming you have a "decrease()" method defined in HealthBar!
roccoricciardi roccoricciardi

2011/5/23

#
i did it slightly different the way i went about it was that I created 4 different images of the health bar for both of my actors respectively. i then created a class as follows: public class MegaHealth extends Actor { public void act() { healthCheck(); } public void healthCheck(int h) { if(h <= 70) setImage("megahealthgreen.gif"); else if(h <=45) setImage("megahealthyellow.gif"); else if(h <=15) setImage("megahealthorange.gif"); else setImage("megahealthred.gif"); } } i intended to create a new HealthBar in each of my actors that would be using it, and pass in their health as the int h. everything in my scenario works fine besides my health bars, this is the error i get when compiling: healthCheck(int) in MegaHealth cannot be applied to () this means..?
roccoricciardi roccoricciardi

2011/5/23

#
i semi have an idea as to where I'm going wrong, but I'd like a separate and probably more accurate opinion
davmac davmac

2011/5/23

#
You've declared healthCheck as:
    public void healthCheck(int h)
But when you call it from the constructor, you don't pass any parameter value:
    healthCheck();
... it needs a parameter value, but you haven't specified it.
roccoricciardi roccoricciardi

2011/5/23

#
okay, so i took the Super Mario scenario created by Donald Duck and did something similar to what he did with the health bar. and it works fine. however remember when i was trying to remove an actor and replace it with another facing the opposite direction? well everytime i make the switch, the health gets reset, and i totally understand why that's happening, but I'm trying to figure out how to avoid it. i need to somehow have my health status at one location as opposed to having it in both classes. . . . . . . . . question: if i have one actor as a subclass to its counterpart, would they be storing their health value in the same location?
davmac davmac

2011/5/23

#
question: if i have one actor as a subclass to its counterpart, would they be storing their health value in the same location?
No, unless you declare the field as "static", but it's best not to do that if you can avoid it (it's bad from a design point of view). However, you can solve the problem by setting the health level of one actor at the same time as you create it from the other one.
You need to login to post a reply.