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

2015/12/27

GameOver screen after 3 humans died

Ramin Ramin

2015/12/27

#
My subclass HUMAN has two subclasses: MAN and WOMAN First of all if the men get image2 ( the image of an ill man ) they should die. If three men died, the game should stop and the object GameOver which is just an image should appear. The variable is already declared. But it doesn't work.... Even if I pause the game and inspect an object, the variable said 0. The code is written in Men.class
1
2
3
4
5
if (getImage() == image2)
       {
           getWorld().removeObject(this);
           peopleDied = peopleDied + 1;
        }
1
2
3
4
5
6
7
8
public void gameOver()
   {
       if (peopleDied == 3)
       {
            Greenfoot.stop()
            getWorld().addObject(new GameOver(), 392, 296);
       }
   }
The second problem is that i don't want the game stop after three men died but after three humans died.. this variable has to be counted in the WOMAN.class as well. And then they both have to be counted together... something like that if you know what i mean...
danpost danpost

2015/12/28

#
A class int field for the HUMAN class would be sufficient to count the deaths of both the MAN and WOMAN objects:
1
protected static int peopleDied;
Remove the variable from the MAN and MOMAN classes.
Ramin Ramin

2015/12/28

#
I did it but it doesn't work... The Human.class:
1
2
3
4
5
6
7
8
9
10
11
12
protected static int peopleDied;
    /**
     * Act - do whatever the Human wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (peopleDied == 5)
        {
            Greenfoot.stop();
        }
    }
The Man.class:
1
2
3
4
5
if (Greenfoot.getRandomNumber(100)<90 && getImage() == image2)
       {
           getWorld().removeObject(this);
           peopleDied = peopleDied +1 ;
        }
danpost danpost

2015/12/28

#
Please show the code that sets the 'image2' image to the actor.
Ramin Ramin

2015/12/28

#
This works... they are changing their image and they are dying. But the game won't stop because the variable doesn't work...
1
2
3
4
5
6
7
8
9
10
public void infect()
    {
       if (canSee(Virus1.class) && Greenfoot.getRandomNumber(100)<10)
       {
           setImage(image2);
        }
       if (canSee(Virus2.class) && Greenfoot.getRandomNumber(100)<25)
       {
           setImage(image2);
        }
danpost danpost

2015/12/28

#
Ramin wrote...
the game won't stop because the variable doesn't work.
It might be where you are coding it from. If you do not yet have an 'act' method in your subclass of World, move the 'act' method to it. Instead of 'peopleDied', use 'HUMAN.peopleDied' to access the variable. Also, change 'protected' to 'public' on the variable declaration line.
You need to login to post a reply.