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

2012/1/15

having lifes

tylers tylers

2012/1/15

#
how do you have lifes
Morran Morran

2012/1/16

#
Try this: In your Player class, add a "boolean" called "isDead", and add an "int" called "timeToRegenerate". In your Player's "act()" class, do this:
public void act()
{
    if(!isDead) { //if the player is not dead... the player can continue to act
         //put your original act method here!
    }
    else { //if the player has lost a life...
        timeToRegenerate++;
        getImage().setTransparency(0); //make the player invisible when dead
        if(timeToRegenerate > 30) { //if the player has been dead long enough, 
              isDead = false;   //regenerate!
              timeToRegenerate = 0;
              getImage().setTransparency(255);
              setLocation(someX, someY); //where someX and someY are the 
        }                               //the location you want the player to regenerate at.
    }
}
When your player dies, do not remove your player from the world. Just set "isDead" to true.
Duta Duta

2012/1/16

#
You should also have an extra variable (named int lives or similar), where you keep track of how many lives you have lost - something like the following (I'll work it around Morran's act() method):
private int lives = 5;
public void act()
{
    if(!isDead) { //if the player is not dead... the player can continue to act
         //put your original act method here!
    }
    else { //if the player has lost a life...
        if(lives > 0)
        {
            timeToRegenerate++;
            getImage().setTransparency(0); //make the player invisible when dead
            if(timeToRegenerate > 30) { //if the player has been dead long enough, 
                  isDead = false;   //regenerate!
                  timeToRegenerate = 0;
                  lives--;
                  getImage().setTransparency(255);
                  setLocation(someX, someY); //where someX and someY are the 
            }                               //the location you want the player to regenerate at.
        }
        else
        {
            getWorld().removeObject(this); //die. add whatever extra code you want here.
        }
    }
}
xChocoMel xChocoMel

2012/5/25

#
Hello, I have the same problem and I'm trying to use thise code, but I can't find the boolean isDead and the int timeToRegenerate anywhere?! They're not in the standard Greenfoot methode (I think) and I dont know how to find it/make it? Can anyone help me please? :) I have a player that has to lose a life when he/she falls down (at x=o). He/she needs to have 3 lifes in total. With the first two lifes lost she needs to start at the beginning of the game. When the third life is lost the game needs to stop! (or go to the last world which says something like GAME OVER). Help, anyone? :)
davmac davmac

2012/5/25

#
Like Morran said:
In your Player class, add a "boolean" called "isDead", and add an "int" called "timeToRegenerate"
To declare those variables you'd use code something like:
private boolean isDead = false;
private int timeToRegenerate = 0;
As these are to be instance variables, they should be declared inside the class but not inside any method!
You need to login to post a reply.