Hi,
I'm writing a little 2d java zombie apocalypse game where the player moves around the screen and shoots zombies that spawn. I have done most of the logic except for the zombie AI. I need them to follow the player slowly.
Right now I am using the same code for bullets as they're similar really. They both have a start and end point.
The current code is:
AdvanceWalkPhas e() gets fired every game tick.
This works but the zombie moves so fast that it appears to just be on top of the player and move with them.
Please can someone help as I'm a little stuck on what to do. I've tried reduce the amount of times the event is fired but it just makes it jerky and it seems to stop before it reaches the player.
I'm writing a little 2d java zombie apocalypse game where the player moves around the screen and shoots zombies that spawn. I have done most of the logic except for the zombie AI. I need them to follow the player slowly.
Right now I am using the same code for bullets as they're similar really. They both have a start and end point.
The current code is:
Code:
public void AdvanceWalkPhase()
{
int a = _target.getLocation().getX() - _location.getX();
int b = _target.getLocation().getY() - _location.getY();
int sum = a ^ 2 + b ^ 2;
if(sum < 0)
{
sum = sum * -1;
}
Steps = (int) Math.sqrt(sum);
diffX = _target.getLocation().getX() - _location.getX();
diffY = _target.getLocation().getY() - _location.getY();
if(Steps == 0)
{
Steps = 1;
}
moveX = (int) (diffX / Steps);
moveY = (int) (diffY / Steps);
Location newLoc = new Location(_location.getX() + (int)moveX, _location.getY() + (int)moveY);
_location = newLoc;
}
This works but the zombie moves so fast that it appears to just be on top of the player and move with them.
Please can someone help as I'm a little stuck on what to do. I've tried reduce the amount of times the event is fired but it just makes it jerky and it seems to stop before it reaches the player.
Comment