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?


//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; } }
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); }
//in the act method of your lobster class (where also the move(double speed) method is in); move(speed);
//in your Lobster class; //... private int speed;// = the starting speed of your lobster; public void incrementSpeed(int speed) { this.speed += speed; }