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

2019/5/3

Timed Power Up Help

luismystic luismystic

2019/5/3

#
i am creating a game and i currently have a power up. when the user eats the power up the game increases in speed, it is currently a permanent speed up and i have the code that resets the speed back to normal when it is restarted. however, i want to make it so the speed only increases for 10 seconds rather than for the rest of the current instance. this is the code for when my character eats the power up:
if (canSee(PowerUp.class))
        {
            eat(PowerUp.class);
            Greenfoot.setSpeed(57);
        
        }
thanks for any help.
danpost danpost

2019/5/3

#
luismystic wrote...
i want to make it so the speed only increases for 10 seconds rather than for the rest of the current instance.
Add a timer field. Set it to 800 or so when powerup is eaten. Separately, in act, if timer is greater than zero, decrease it and if, upon that, it becomes zero, reset the speed:
// add field
private int speedTimer;

// in act
if (speedTimer > 0 && --speedTimer == 0) Greenfoot.setSpeed(50);
if (cansee(PowerUp.class))
{
    eat(PowerUp.class);
    Greenfoot.setSpeed(57);
    speedTimer = 800; // adjust as needed
}
You need to login to post a reply.