Search code examples
javaintdoublegame-physics

setting enemy movement speed to less than 1


In the game that I'm creating I want the zombies to be twice as slow as the player put I don't want to set the players movement speed to 2 because it moves way to fast then. This is the code that controls the zombies speed:

Zombie z = (Zombie) zombie.get(i);
if(z.getY() > player.getY()){
        z.setY(z.getY() - 1);
}
if(z.getY() < player.getY()){
        z.setY(z.getY() + 1);
}
if(z.getX() > player.getX()){
        z.setX(z.getX() - 1);
}
if(z.getX() < player.getX()){
        z.setX(z.getX() + 1);
}

I have tried to use (int) .5f, (int) .5 and 1 / 2 but all of them makes the zombie stand completly still.


Solution

  • I think that what you want to do is to declare your Zombie's x and y fields as doubles, not ints, and thus the setX(...) and setY(...) methods would accept double and the getX() and getY() return doubles. If you need int values for your imaging then you can either cast or round the values at the time that this is needed, or give Zombie methods to do this, getIntValueX() and getIntValueY().

    Also you could just use a single Point2D.Double field in place of your x and y fields.