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

2012/1/21

Health Hearts!

Razzo Razzo

2012/1/21

#
How can I display the current health of a Player in Hearts, This is what I have, but it's not changing
       for(int y = 0; y < health; y++) 
        getWorld().addObject(new Heart(), 10+(y*10),390);
My Heart class is just an Image.
sp33dy sp33dy

2012/1/21

#
Hi, Not entirely confident I know what you are trying to do, but I'll ask some simple questions: 1) Are you sure 'health' has a value in it?! 2) Have you made sure the image is allocate to Heart() actor? I.E. it's not set to no image 3) Have you checked that the height of 390 is within your world boundaries Other than that, i'd need to see more code to help.. Kind regards Sp33dy
Razzo Razzo

2012/1/21

#
No, I mean It shows at the start, but as the Health decreases the objects dont.. Im not sure how to do that.. D:
sp33dy sp33dy

2012/1/21

#
Ah, ok. You've got one of two options: - Either change the image in the related object to something empty. In actor you could getImage() and then issue clear() for instance. or - Remove the object from the world. You'll need the world object and then issue removeObject(object) So, either way, you'll need to keep a reference to each of the Heart objects you are adding. As you'll need to be able to remove the one on the end. As I'm still becoming familir with Greenfoot, I'm not sure what 'List' type objects there are. However, you could look at the java.util.Vector class. This allows you to add an object into a Vector (like an address book) and then get item one, two or last item back. Go see my Lines demo code I've just put up. It clearly shows you how to add and retrieve objects. I hope this helps... regards sp33dy
kiarocks kiarocks

2012/1/21

#
Also, you keep adding objects but never removing, you will end up with over 10000 objects a t one point...
kiarocks kiarocks

2012/1/21

#
And we do need more code to solve this.
Razzo Razzo

2012/1/21

#
thats pretty much all I have ...
kiarocks kiarocks

2012/1/21

#
Well that is a piece of code, we need the code from the actor or world this snippet is in.
davmac davmac

2012/1/21

#
Simple solution: change your code to read:
getWorld().removeObjects(getWorld().getObjects(Heart.class));
for(int y = 0; y < health; y++)   
 getWorld().addObject(new Heart(), 10+(y*10),390);
This assumes you're using the hearts only to display the number of lives, and not for anything else (since it removes all hearts, indiscriminately).
sp33dy sp33dy

2012/1/21

#
davmac, Great thinking and that should have dawned on me to just remove them all before showing again! It's hard to look at some of these problems cold, but want to try to learn how to help others.
davmac davmac

2012/1/21

#
sp33dy, that's no problem. It's good to have people who are willing to try and help others!
You need to login to post a reply.