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

2015/12/27

Set lifetime of an object

Ramin Ramin

2015/12/27

#
I have a game where viruses infect humans and the player can control an injection to heal the humans. When the humans get infected they switch their image to symbolize that they are ill. I want something like: if that person is not healed after 30 seconds, he dies (the world removes that object). How can I do that?
danpost danpost

2015/12/28

#
Ramin wrote...
I want something like: if that person is not healed after 30 seconds, he dies (the world removes that object). How can I do that?
You need to start a timer when the human is infected. Its initial value should be something like 1600 to 1800 if your scenario is running at a moderate speed (speed slider around the middle). Two condition will be required to remove the human -- (1) image is 'sick' image; and (2) counter is decreased to zero.
Ramin Ramin

2015/12/28

#
How can I start a timer like that? Can you show me because 'm a newbie in greenfoot.
Ramin Ramin

2015/12/28

#
That's the code i have in man.class. The third if condition is that an ill human should die. At the moment it's with the Greenfoot.getRandomNumber() method, so it's a propability of 10% that the man dies. IMPORTANT: there are several men-objects. So every object has to have a timer for itself. So now i want to change this probability thing (the getRandomNumber() method) to a timer...
    public void infect()
    {
       if (canSee(Virus1.class) && Greenfoot.getRandomNumber(100)<10)
       {
           setImage(image2);
        }
       if (canSee(Virus2.class) && Greenfoot.getRandomNumber(100)<25)
       {
           setImage(image2);
        }
       if (Greenfoot.getRandomNumber(100)<10 && getImage() == image2)
       {
           getWorld().removeObject(this);
           peopleDied = peopleDied +1 ;
        }
    }
Ramin Ramin

2015/12/28

#
And there are several viruses. So if the timer is running, it should not start from the beginning when the object hits another virus... Because it hits like every 5 seconds randomly another virus and if that happens and the timer would start again and again, it would never reach the 30 seconds...
danpost danpost

2015/12/28

#
Basically, you would have this:
// instance field
private int timer;

// starting timer
setImage(image2);
if (timer == 0) timer = 1800;

// running timer
if (timer > 0)
{
    timer--;
    if (timer == 0)
    {
        getWorld().removeObject(this);
        peopleDied++;
    }
}
Also, if injected, the timer field should be reset to zero while resetting the image to 'image1'.
Ramin Ramin

2015/12/28

#
do you mean
 if (timer==0)
{
timer=1800
}
(line 6)
danpost danpost

2015/12/28

#
Ramin wrote...
do you mean
 if (timer == 0)
{
    timer=1800
}
(line 6)
One in the same. One-liner 'if' statements can be written like that; although, it is not considered a good practice to do so. The reason is that if more needed to be added within that 'if' "block", it is easy to miss the way it is written and mess up with the added code.
Ramin Ramin

2015/12/29

#
ok thank you very much
Ramin Ramin

2016/1/2

#
How can I reset the timer?
if (canSee(Injection.class)
{
timer=0
}
like this??
danpost danpost

2016/1/2

#
Ramin wrote...
How can I reset the timer?
if (canSee(Injection.class)
{
timer=0
}
like this??
Something like that. You might want to 'eat' the Injection object also.
You need to login to post a reply.