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

2014/10/27

Need help with creating lives counter

A1234 A1234

2014/10/27

#
Hello, so I am making a platform game and need help with creating a lives counter. What I am hoping is to have individual hearts for each life and as my player loses a life, the image of one of the hearts will also disappear as well if that makes sense? So say I start with 3 lives and it displays in the world with three hearts, then when my player does something to lose a life, they will lose a life but then it will also make one of the hearts disappear and leave only 2, and so on. Then, when there are no lives left, for it to display my game over message (I have already created this) Thanks in advance :)
classicjimmy classicjimmy

2014/10/27

#
Store the amount of lives left in a static variable and remove the hearts you inserted based on the value of that static variable. Let's say the player has 3 lives:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static int lives;
 
addObject(new Life(), x1, y1);
addObject(new Life(), x2, y2);
addObject(new Life(), x3, y3);
...
 
public void handleLives()
 
if (lives == 2) getWorld().removeObjects(getWorld().getObjectsAt(x3, y3, Life.class));
 
if (lives == 1) getWorld().removeObjects(getWorld().getObjectsAt(x2, y2, Life.class));
 
if (lives == 0) getWorld().removeObjects(getWorld().getObjectsAt(x1, y1, Life.class));
You would obviosuly need to increment the variable everytime the player loses a life, and also call handleLives( ) in the act method.
Super_Hippo Super_Hippo

2014/10/27

#
You really don't need to call 'handleLives' in the 'act' method. It is enough to be executed when a life is lost. And it would also be better if you have a reference to the hearts so you only remove them. If another object is at the position of the heart, it would be removed. This could happen especially when called in the act method like this.
danpost danpost

2014/10/27

#
Super_Hippo wrote...
If another object is at the position of the heart, it would be removed. This could happen especially when called in the act method like this.
It was specified that the retrieved objects at location be Life objects, so no other object could be removed.
Super_Hippo Super_Hippo

2014/10/27

#
Oh you are right, sorry for that.
danpost danpost

2014/10/27

#
@Super_Hippo, no need to be sorry. Any and all help is, to be sure, appreciated. It does not hurt to have inaccuracies thrown in, provided they are corrected; they can help others, as well as the one posing the current issue, and yourself as well. It is in my nature to be as clear and precise as possible, as well as including any exceptions or inconsistencies. This is in an attempt to avoid any pre-misconceptions.
A1234 A1234

2014/10/27

#
Thank you everyone for your responses! @classicjimmy, do I add that code to my player class?
classicjimmy classicjimmy

2014/10/28

#
Yes, add the handleLives method in the player class and call it whenever the player loses a life. The life objects (hearts) you can add in a world subclass or in the player class itself by putting getWorld(). in front of every addObject line.
A1234 A1234

2014/10/28

#
Alright, thank you! What do I need to do after this to actually make it so that my player loses lives after touching certain objects? Sorry, I'm hopeless when it comes to counters
danpost danpost

2014/10/28

#
A1234 wrote...
then when my player does something to lose a life, they will lose a life but then it will also make one of the hearts disappear and leave only 2, and so on. Then, when there are no lives left, for it to display my game over message (I have already created this)
In pseudo-code:
1
2
3
4
5
if player does something
{
    decrement lives
    handle lives
}
I will not tell you where it will go; but, I will say that this is something that will have to be checked constantly while the player is in the world.
A1234 A1234

2014/11/2

#
Sorry for the late reply, I mustn't have pressed post on my last reply. I have most of it working fine, but I am stuck with writing when my player does something, a life will be lost. Can you please help me with that? Thank you!
You need to login to post a reply.