I am working on a basic AI system to get an entity to follow a player. I created both the player and the ghost entity, but it seems as if the ghost only wants to increment both x and y, it never wants to do the opposite, or choose only x or y to use. Here is where I implemented the ghost and player in my constructor:

public MansionAzazel() {
    TileCoordinate ghostSpawn = new TileCoordinate(52, 50);
    TileCoordinate playerSpawn = new TileCoordinate(54, 52);
    player = new Player(playerSpawn.x(), playerSpawn.y(), key);
    ghost = new Ghost(ghostSpawn.x(), ghostSpawn.y(), player);

Here is the update() function for the ghost:

public void update() {
    int xa = 0, ya = 0;
    if (anim < 1000000)
        anim = 0;

    if (player.getX() > xa)
    if (player.getX() < xa)
    if (player.getY() > ya)
    if (player.getY() < ya)

    if (xa != 0 || ya != 0) {
        move(xa, ya);
        walking = true;
    } else
        walking = false;

In which to sum it all up, the ghost entity will only and always move in one direction because the coordinates are 0 and I don't know why.

  • 1
    \$\begingroup\$ What is your question? \$\endgroup\$
    – wes
    Aug 12, 2013 at 18:50
  • \$\begingroup\$ It looks like your ghost update function uses player.getX() when checking for Y coordinates. You might want to fix that. Also, you should output what player.getX() and player.getY() returns in your ghost update function to see what you get. \$\endgroup\$ Aug 12, 2013 at 22:01
  • \$\begingroup\$ @UnderscoreZero I said that it returns -1 for both of them. I \$\endgroup\$
    – Shzylo
    Aug 12, 2013 at 22:04
  • \$\begingroup\$ What I meant was change the line System.out.println(xa + ", " + ya); to System.out.println(xa + ", " + ya + " " + player.getX() + ", " + player.getY()); \$\endgroup\$ Aug 12, 2013 at 22:14
  • \$\begingroup\$ @UnderscoreZero after I changed getX to getY from checking y coordinate they both print out positive 1 Also, I divided the player.getX() and player.getY() by 16 (tile size) it prints out the actual location, just not ghost. \$\endgroup\$
    – Shzylo
    Aug 12, 2013 at 22:14

1 Answer 1


If the intent is the move the ghost toward the player and if the move() method moves the ghost (you haven't given us enough info to be sure), then perhaps the update() method should initialize xa and ya to ghost.getX() and ghost.getY(). It seems to reset xa and ya to 0 every time update() is called.

  • 1
    \$\begingroup\$ Based on how his player uses the move() function, that is most likely not going to do what you expect it to do. I am assuming his move function takes the current position and offsets it by xa and ya. This same update function was used for his player and it worked fine. \$\endgroup\$ Aug 13, 2013 at 15:46
  • \$\begingroup\$ Okay, @JMB you were quite right about that. It does reset the variables each time. Instead of using the my variables xa and ya I used this.getX() and this.getY() which worked perfectly! \$\endgroup\$
    – Shzylo
    Aug 13, 2013 at 16:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .