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

2016/9/22

Need help with a Slow Time Power up.

Samas Samas

2016/9/22

#
I've been searching for a way to decrease the speed of a certain actor when another actor picks up a power up. I want my Sheep (player) to be able to pick up a power up that will slow down the movement speed of the wolves (enemies) chasing it. Any help is much appreciated
Super_Hippo Super_Hippo

2016/9/22

#
Sheep:
//when getting powerup
Wolf.slow = true;
//use some variable as a timer and when the power up time is over (unless it should be for ever)
Wolf.slow = false;
Wolf:
public static boolean slow = false;
private int slowTimer = 0;

//beginning of act method
if (slow)
{
    if (slowTimer > 0)
    {
        slowTimer--;
        return;
    }
    slowTimer = 1; //the higher the number, the slower the wolves
}
//rest of the act method
This will make the Wolves act half as fast. ---------------------------------- Another way is to just change the speed of the movement. (Maybe it should extend the SmoothMover class.) Sheep:
//collected power up
Wolf.speed *= 0.8; //set the speed to 80 %

//power up over
Wolf.speed *= 1.25; //set it back to 100 %
Wolf:
public static double speed = 5; //current speed ("100 %")

//when moving
move(speed);
When using static fields, they should be set to the correct value in the world constructor because they are not recreated when resetting the scenario.
Samas Samas

2016/9/22

#
Is there a way to make the move() a double? When I try to use the double speed it says : "incompatible types: possible lossy conversion from double to int."
Samas Samas

2016/9/22

#
Super_Hippo wrote...
Sheep: Wolf:
public static double speed = 5; //current speed ("100 %")

//when moving
move(speed);
on this part
danpost danpost

2016/9/22

#
You could try the following for line 4:
move((int)speed);
Samas Samas

2016/9/23

#
Okay that worked. How do I set a timer for the how long the wolf is to be slowed? For now the wolf is slowed forever
danpost danpost

2016/9/23

#
Samas wrote...
How do I set a timer for the how long the wolf is to be slowed? For now the wolf is slowed forever
You can do it with something like this:
/**  in Sheep class act method (or method it calls) */
if (<collected slow powerup>)
{
    Wolf.slowDown();
}

/** in Wolf class */
public static boolean slow;
public static int slowTimer;

public static void slowdown()
{
    if (!slow)
    {
        slow = true;
        slowTimer = 200; // adjust value as needed
    }
}

/** in World class act method (or a method it calls) */
if (Wolf.slow)
{
    Wolf.slowTimer--;
    if (Wolf.slowTimer == 0) Wolf.slow = false;
}

/** in Wolf class act method (or a method it calls) */
if (slow) move(3); else move(5);
danpost danpost

2016/9/23

#
Actually, the boolean field is not needed. The following should suffice:
/** in Sheep class act method (or method it calls) */
if (<collected slow powerup>)
{
    Wolf.slowTimer = 200; // adjust value as needed
}

/** in Wolf class */
public static int slowTimer;

/** in Wolf class act method (or a method it calls) */
if (slowTimer > 0) move(3); else move(5);

/** in World class act method (or a method it calls) */
if (Wolf.slowTimer > 0) Wolf.slowTimer--;
Samas Samas

2016/9/23

#
Thank you so much! it worked perfectly!
You need to login to post a reply.