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

2017/3/6

Lifebar in Greenfoot

elroxar elroxar

2017/3/6

#
Hey guys. I made a game in Greenfoot and now I want to display a bar with the life in the corners. Each character has six lives so there is a total of 6 different bars. I made a own class for each bar. In the world I made a switch statement to add the bar object depending on the life.
    public void Leben(int H, int O)
    {
        switch(H)
        {
            case 0 :
                addObject(new H_0(), 365, 59);
                break;
                
            case 1 :
                addObject(new H_1(), 365, 59);
                break;
                
            case 2 :
                addObject(new H_2(), 365, 59);
                break;
                
            case 3 :
                addObject(new H_3(), 365, 59);
                break;
                
            case 4 :
                addObject(new H_4(), 365, 59);
                break;
                
            case 5 :
                addObject(new H_5(), 365, 59);
                break;
        }
        switch(O)
        {
            case 0 :
                addObject(new O_0(), 862, 59);
                break;
                
            case 1 :
                addObject(new O_1(), 862, 59);
                break;
                
            case 2 :
                addObject(new O_2(), 862, 59);
                break;
                
            case 3 :
                addObject(new O_3(), 862, 59);
                break;
                
            case 4 :
                addObject(new O_4(), 862, 59);
                break;
                
            case 5 :
                addObject(new O_5(), 862, 59);
                break;
        }
    }
I tried it several time, in other ways too but it doesn't work. Any suggestions? Thanks
Super_Hippo Super_Hippo

2017/3/6

#
Do not create an own class for every life bar. Add a new instance of life bar for every character into the world once when the game starts. When a character loses a life, tell its life bar to change its image accordingly to the lives left.
Nosson1459 Nosson1459

2017/3/7

#
You will only get either 1 or 0 bars since in each case in the switch statement there is only 1 addObject.
Super_Hippo wrote...
Do not create an own class for every life bar. Add a new instance of life bar for every character into the world once when the game starts. When a character loses a life, tell its life bar to change its image accordingly to the lives left.
You need to login to post a reply.