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

2016/5/22

Problem with Live counter

LittleMiiMe LittleMiiMe

2016/5/22

#
Hello there I'm back again with another problem I'm now working on a life counter. I don't know if it counts and it doesn't change the images I made.
public class Counter extends Actor
{
    private int live = 3;
    private int fault = 2;
    private int aaaa = 0;
    private static final int faultF = 2;
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        CalcF();
        
        GreenfootImage image = new GreenfootImage("bbbOrgF11.png");
        
        if(fault <= 0)
        {
            live--;
            fault = 3;
            aaaa = 0;
        }

        if (live == 3)
        {
            image = new GreenfootImage("bbbOrgF11.png");
        }
        else
        if (live == 2)
        {
            image = new GreenfootImage("bbbOrgF22.png");
        }
        else
        if (live == 1)
        {
            image = new GreenfootImage("bbbOrgF33.png");
        }
        else
        if (live == 0)
        {
            image = new GreenfootImage("bbbOrgF44.png");
        }
        
        setImage(image);
        
        if (live == 0)
        {
            getWorld().addObject(new Message(), getWorld().getWidth()/2, getWorld().getHeight()/2);
            Greenfoot.stop();
        }
    }
    
    private void CalcF()
    {
        fault = faultF - aaaa;
    }
    
    public void sub()
    {
        aaaa++;
    }
}
- create vars - create base var for Image - if fault counter hits specific number (3) - Life counter subs by one - Fault counter gets reset - other fault counter gets reset - If live counter hits specific number (number of lives) a new Image will be set in image var - Set the var image as image - if No lives are left - show message "game over" - Stop - Fault counter = static number of faults (3) - other fault counter - Other fault counter gets added with 1 if that methode gets called by another actor Is there a problem with the counter it self or the thing to set the images? Kind regards Yen
danpost danpost

2016/5/22

#
Things to make things easier: (1) add a constructor to set the initial image (or set the initial image as the default image for objects of the class); (2) set the appropriate image immediately after changing the 'live' value instead of every act cycle; the check for game over can be done there also; (3) there is no need for an 'act' method in the class; everything is done when the counter is created or when a field changes its value due to other classes calling for it. In other words, you only need the constructor to set the initial values and image and a method that is called from outside the class to adjust a value, at which point the value is checked to possibly adjust the lives, set the new image and check for game over. I do not think you need the 'faultF' field to be 'static'. You probably only ever have one created object from the class at a time, anyway -- and as 'private' with no 'getter' method, no other class can access its value.
You need to login to post a reply.