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

2017/10/17

Timer for integers

monmonmon monmonmon

2017/10/17

#
Hi, Im trying to make an "EnergyDrink" to my game which adds speed to the Person, when the Person drinks the EnergyDrink, I want the speed added to the Person temporary only, or just for only 15 seconds, I tried to create a timer, but Im not really good with timers so I need help how to set the speed added for only 15 seconds, here's my initial code :
public class Person extends MainActors
{
           public int speed = 2;
           private int timer;

public void act() 
    {    
           drink();
     }
public void drink()
    {
        Actor energydrink = getOneIntersectingObject(EnergyDrink.class);

        if (energydrink!= null)
        {
            speed++;
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            HealthBar healthbar = myworld.getHealthBar();
            healthbar.gainHealth();
            myWorld.removeObject(energydrink);

        }
        if (timer>0)
        {
            timer--;
            if(timer == 0) speed--;
        }
    }
}
danpost danpost

2017/10/17

#
You just forgot to set a positive value (maybe 900) to the timer when drinking.
monmonmon monmonmon

2017/10/17

#
If what you meant was the value in line 27, I changed 0 to 900 but the speed added still won't decrease
danpost danpost

2017/10/17

#
monmonmon wrote...
If what you meant was the value in line 27, I changed 0 to 900 but the speed added still won't decrease
No -- that is not what I meant. You need to set it to some positive value so that line 26 can decrease it back to zero -- and this needs to be done when the person gets the energy drink.
monmonmon monmonmon

2017/10/17

#
I set the value of private int timer = 900, then put lines 24-28 to line 22, then when I get the EnergyDrink, the speed still doesnt speed down
danpost danpost

2017/10/17

#
monmonmon wrote...
I set the value of private int timer = 900, then put lines 24-28 to line 22, then when I get the EnergyDrink, the speed still doesnt speed down
Again -- not correct. Just put:
timer = 900;
at line 22 above.
monmonmon monmonmon

2017/10/18

#
oh sorry haha and thanks
You need to login to post a reply.