I am attempting to make a game where police cars follow a 'player' car that is controlled using arrow keys. I used the turnTowards method to achieve this, but the police cars turns extremely fast. I am trying to make it so the police cars are faster than the player car, but the police cars turn slower than the player car, so that the police cars can be dodged by the player car. Is there any way to make an object turn at a specified speed that is following another object? I have already tried using a timer, but now the turning of the police cars are choppy and not smooth anymore. I am still a beginner, so thanks for any help. Here is my code:
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | public class Police extends Actor { private int delay; private static final int DELAY = 20 ; boolean isTouchingEdge() { //checks if car object(s) are touching edge of world if ( this .getX() == 0 ) { this .setLocation( 590 , this .getY()); } if ( this .getX() == 599 ) { this .setLocation( 10 , this .getY()); } if ( this .getY() == 0 ) { this .setLocation( this .getX(), 390 ); } if ( this .getY() == 399 ) { this .setLocation( this .getX(), 10 ); } return true ; } /** * Act - do whatever the Police wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move( 5 ); isTouchingEdge(); if (delay < DELAY) { delay++; return ; } else { delay = 0 ; Actor car = (Actor)getWorld().getObjects(Car. class ).get( 0 ); turnTowards(car.getX(), car.getY()); isTouchingEdge(); } } } |