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

2016/3/9

Finishing touches

starworld31 starworld31

2016/3/9

#
SO my game is almost done , I jut need to know how to add a visual life counter for the princess so that the player can monitor their lives. ( My game is a tower defense game btw and the princess will be the one getting hit) . Where do I put the code for that string (if I'm not mistaken. Attached are the my world codes and the princess codes for her life.
public MyWorld()
    {    
       super(1000, 700, 1);        
        setBackground("bg2.jpg");
        
        addObject(new Princess(), 352,594);
        addObject(new Infantry(), 344,309);
        addObject(new arm(), 369,331);
    }
    
    public void act()
    {
        if(Greenfoot.getRandomNumber(100)<1)
        {
            addObject(new Enemy(), 999, 594);
        }
    }
}
For the princess :
public class Princess extends Actor
{
    public int life = 20;
    public Princess()
    {    
        GreenfootImage image = getImage();  
        image.scale(75, 100);
        setImage(image);
             
    }
    
    public void act() 
    {
        if (getOneIntersectingObject(Enemy.class) != null) {
            life--;
            removeTouching(Enemy.class);
            if(life==0)
                Greenfoot.setWorld(new GameOver());
         }
    } 
    
}
danpost danpost

2016/3/9

#
You can look at my Value Display Tutorial to learn how to accomplish that.
You need to login to post a reply.