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

2013/2/7

how to increase speed of lobster

kingenzo kingenzo

2013/2/7

#
in my game the crab eats worms, and the lobster eats crab. I want to make the lobsters move faster for every worm the crabs eat, but how?
Gevater_Tod4711 Gevater_Tod4711

2013/2/7

#
Well that is a little difficult. You need to change the speed of your lobster by getting his reference and changing the value. I don't know how the lobster moves but you basicly need this here:
//in your Crab class:
import java.util.List;

//...

//in your eatWorm method (or however it's called);
public void eatWorm() {
    //...
    //after eating the worm;
    List<Lobster> lobsters = getWorld().getObjects(Lobster.class);
    for (Lobster lobster : lobsters) {
        lobster.incrementSpeed(1);//or a higher value than 1 if you want to make him much faster;
    }
}



//in your Lobster class;

//...
private int speed;// = the starting speed of your lobster;

public void incrementSpeed(int speed) {
    this.speed += speed;
}

}
Then you just need to makt your lobster move using the speed value. For example you could use a method like this:
public void move(double speed) {
    double angle = Math.toRadians( getRotation() );
    int x = (int) Math.round(getX() + Math.cos(angle) * speed);
    int y = (int) Math.round(getY() + Math.sin(angle) * speed);
    setLocation(x, y);
}
Call the method like this:
//in the act method of your lobster class (where also the move(double speed) method is in);
move(speed);
kingenzo kingenzo

2013/2/7

#
he says he doesnt know the method incrementSpeed() ??
-nic- -nic-

2013/2/7

#
you have to create the incrementSpeed() method yourself by putting
//in your Lobster class;  
  
//...  
private int speed;// = the starting speed of your lobster;  
  
public void incrementSpeed(int speed) {  
    this.speed += speed;  
}  
  
in the lobster class
kingenzo kingenzo

2013/2/7

#
thanks!!
kingenzo kingenzo

2013/2/7

#
is there something to make them increase speed a little slower, for (Lobster lobster : lobsters) { lobster.incrementSpeed(1);//or a higher value than 1 if you want to make him much faster; I want not to make him much faster but make him daster, but slower faster
danpost danpost

2013/2/7

#
What you are asking is for the acceleration to be lower, but the range of speed basically stay the same. One way to accomplish this is to multiply the speed by some number (let us say, 10), but move by one-tenth that amount (speed/10). This way the accceleration is actually one-tenth of what is was before. The maximum value of speed will have to be multiplied by ten also to mantain the initial maximum speed. I believe my Teleport Demo scenario would be a good example of how this works.
danpost danpost

2013/2/7

#
Edit on last post: had wrong scenario listed; it has been corrected. The scenario uses a slowdown rate of one (true slowdown value of 1/100) and an acceleration rate of twenty (true acceleration value of 20/100 ).
You need to login to post a reply.