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

2013/5/7

Does anyone know how to make something slow down if it touches a certain area?

Jimmy.N Jimmy.N

2013/5/7

#
Making game when I came across the problem. Wondered if anyone could help me to solve it to make my game better.
Kartoffelbrot Kartoffelbrot

2013/5/7

#
If you use the move()-method you can di that with a variable.
int speed = 5;
public void act()
{
move(speed);
checkSlow();
}

public void checkSlow()
{
if(inCertainArea())//here you have to check, if the object is in the area you mean
{
speed=3; //what the speed should be in slow-mode
}
else
{
speed=5;
}
}
danpost danpost

2013/5/7

#
Add an instance int field to hold a delay-timer. If in slow-down area, set it to some positive value. Take the code in your act method that makes the actor move and enclose it as follows:
// using the following declared instance field
private int delayTime = 0;

// in the act method (or in the move method itself, if you have one)
if (delayTime == 0)
{
    // code to move actor (or a call to a method to do so, if you have one and not already there)
    if (/* inSlowArea */) delayTime = 1; // the speed will be 1/(n+1) normal rate for whatever n you use
} else delayCount--;
danpost danpost

2013/5/7

#
Kartoffelbrot's code would work best if your actor moves faster than 1 pixel per move. My code would work best if your actor is moving only 1 pixel per move already. If you want more range at speeds slower than one pixel per move (other than 1/2, 1/3, 1/4, 1/5 speed, etc), you will have to use instance double fields to track your actors x and y locations, adjust them programmatically and move your actor accordingly using (int)x and (int)y.
You need to login to post a reply.