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

2013/9/13

The current code im using for my character,how do i add lives?

CreepingFuRbalL CreepingFuRbalL

2013/9/13

#
public class PBear extends Actor
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    
    { 
      moveAndTurn();
      eat();
      
    }
    public void moveAndTurn()
    {
    
            
      
        move(4);
        
        if (Greenfoot.isKeyDown("A"))
        {
            turn(-3);
          
   
    }    
        if (Greenfoot.isKeyDown("D"))
        {
            turn(3);
    }
}
public void eat()
{
    Actor GFish;
     GFish = getOneObjectAtOffset(0,0, GFish.class);
    if ( GFish != null)
    {
        World world;
        world = getWorld();
        world.removeObject( GFish);
    }

}
}
Gevater_Tod4711 Gevater_Tod4711

2013/9/13

#
To add lives in your character class you first need a variable to save the current live state. Now you need to count down this value when your actor looses lives and check whether the actor has no lives anymore. When do you want your actor to loose a live?
CreepingFuRbalL CreepingFuRbalL

2013/9/13

#
I want my actor to loose a live when he is hit by another actor what code would i use to add the variable for lives?
Gevater_Tod4711 Gevater_Tod4711

2013/9/13

#
To add the variable you can use this code:
public class PBear extends Actor  
{
    private int lives = 10;//or any other starting number;

    //the rest of your code;
}
To make your actor loose a live when it hits another actor is easy. Try it like this:
//put this code in the act method of your PBear calss;
if (getOneIntersectingObject(OtherObject.class) != null) {
    lives--;
}
The only problem is that after you lost a live the actor will still be there and your actor will get hurt again. Do you want to remove the other actor after it has hit your actor? (that would be the easyest way) If so you can use this code:
//put this code in the act method of your PBear calss;
Actor intersectingEnemy= getOneIntersectingObject(OtherObject.class)
if (intersectingEnemy!= null) {
    lives--;
    getWorld().removeObject(intersectingEnemy);
}
You need to login to post a reply.