title: java: Enemy player tracking AI for java game
body: <p>I am creating a game for the first time in java and I'm currently trying to get my zombies to track and follow the player. below is my code. I have created a controller class, and used a linked list to create multiple zombies. Then in my zombies update method I use simple if statements for the zombie to track the player. The problem is that the zombies track to the players initial starting position, but not to where he is moved. </p>
  
  <p>In the zombie update() method I used a System.println(player.getX()); and it showed that the players position was not being updated in the zombies class, so that is why they only track to its starting position. I cant figure out how to fix this. any help would be appreciated.</p>
  
  <p>ZOMBIE CLASS:</p>
  
  <pre><code>public class Zombie extends Entity{
  
  Animation dwn,up, left, right;
  BufferedImage north, south, east, west;
  
  Sprites sprites = new Sprites();
  Player player = new Player(100,100);
  
  
  Rectangle boundsZombie;
  
  String direction = "";
  
  
  public Zombie(int x, int y){
  
      super(x,y);
      sprites.create();
      boundsZombie = new Rectangle(0,0, 32, 32);
  
  }
  
  public void update(){
  
      if(player.getX() &gt; x){
          x = x + 1;
      }
  
      if(player.getX() &lt; x){
          x = x - 1;
      }
  
      if(player.getY() &gt; y){
          y = y + 1;
      }
  
      if(player.getY() &lt; y){
          y = y - 1;
      }
  
      System.out.println(player.getX());
  }
  
  
  
  public Rectangle getBounds(){
      return new Rectangle((int) x, (int) y, 32, 32);
  }
  
  public void render(Graphics g){
  
      g.drawImage(sprites.zombieNorth, x, y, null);
      //g.setColor(Color.red);
      //g.fillRect((int) x + boundsZombie.x, (int) y + boundsZombie.y, boundsZombie.width, boundsZombie.height);
  
  }
  
  public int getX() {
      return x;
  }
  
  public void setX(int x) {
      this.x = x;
  }
  
  public int getY() {
      return y;
  }
  
  public void setY(int y) {
      this.y = y;
  }
  
  }
  </code></pre>
  
  <p>PLAYER CLASS</p>
  
  <p>public class Player extends Entity{</p>
  
  <pre><code>Animation dwn,up, left, right;
  BufferedImage north, south, east, west;
  Sprites sprites = new Sprites();
  KeyManager keyManager = new KeyManager();
  Rectangle boundsPlayer;
  //Controller c;
  
  
  boolean movUp, movDwn, movRight, movLeft;
  String direction = "";
  
  
  
  public Player(int x, int y){
      super(x,y);
      sprites.create();
      dwn = new Animation(100, sprites.player_down);
      up = new Animation(100, sprites.player_up);
      left = new Animation(100, sprites.player_left);
      right = new Animation(100, sprites.player_right);
      north = sprites.playerNorth;
      south = sprites.playerSouth;
      east = sprites.playerEast;
      west = sprites.playerWest;
  
      boundsPlayer = new Rectangle(0,0, 32, 32);
  }
  
  public void setX(int x) {
      this.x = x;
  }
  
  public void setY(int y) {
      this.y = y;
  }
  
  public Rectangle getBounds(){
      return new Rectangle((int) x, (int) y, 32, 32);
  }
  
  /**
   * player tick method, used to move the player
   */
  public void update(){
  
      if(Game.getKeyManager().up){
          y -= 3;
          direction = "north";
  
  
      }
      if(Game.getKeyManager().down){
          y += 3;
          direction = "south";
  
      }
      if(Game.getKeyManager().left){
          x -= 3;
          direction = "west";
  
      }
  
      if(Game.getKeyManager().right){
          x += 3;
          direction = "east";
          //System.out.println(x);
      }
  
  }
  
  public int getX() {
      return (int)x;
  }
  
  public int getY() {
      return (int)y;
  }
  
  public String getDirection(){
      return direction;
  }
  
  /**
   * method used to return players facing direction sprite
   * @return
   */
  public BufferedImage getPlayerDirection(){
      if(direction == "north"){
          return north;
      }
  
      else if(direction == "south"){
          return south;
      }
  
      else if(direction == "east"){
          return east;
      }
  
      else{
          return west;
      }
  
  }
  
  
  public void render(Graphics g){
      g.drawImage(getCurrentAnimationFrame(), (int) x, (int) y, null);
  
      //g.setColor(Color.red);
      //g.fillRect((int) x + boundsPlayer.x, (int) y + boundsPlayer.y, boundsPlayer.width, boundsPlayer.height);
  }
  
  
  /**
   * method used to return the bufferedImage of current frame
   * @return
   */
  public BufferedImage getCurrentAnimationFrame(){
      if(Game.getKeyManager().right == true){
          return right.getCurrentFrame();
      }
  
      else if(Game.getKeyManager().left == true){
          return left.getCurrentFrame();
      }
  
      else if(Game.getKeyManager().up == true){
          return up.getCurrentFrame();
      }
  
      else if(Game.getKeyManager().down == true){
          return dwn.getCurrentFrame();
      }
  
      else {
          return getPlayerDirection();
      }
  
  
  }
  </code></pre>
  
  <p>}</p>