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?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //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; } } |
1 2 3 4 5 6 | 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); } |
1 2 | //in the act method of your lobster class (where also the move(double speed) method is in); move(speed); |
1 2 3 4 5 6 7 8 | //in your Lobster class; //... private int speed; // = the starting speed of your lobster; public void incrementSpeed( int speed) { this .speed += speed; } |