Reputation: 99
I've implemented some pathfinding in my 2d game, but I don't know how to move my 'Zombie' character. I'm using slick2d and everything gets put on the screen in the render method, however I want to update the zombies movement in the Zombie class. Here is the method in the zombie class:
public void findPrey(){
Pathfinder pf = new Pathfinder();
pf.setStartNode(xPosition,yPosition);
for(PathCoordinates p: pf.calculatePath()){
if(p.getXPosition() > xPosition){
moveRight(2);
}if(p.getXPosition() < xPosition){
moveLeft(2);
}if(p.getYPosition() > yPosition){
moveUp(2);
}if(p.getYPosition() < yPosition){
moveDown(2);
}
}
}
So I want to loop through the List of x and y coordinates and move the zombie. However nothings happens- the zombie doesnt move at all.
In my 'world' class I have the render method, which loops through the zombies and renders them, the code has been edited slightly:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
throws SlickException {
for (Zombie z : zombies) {
if (!z.isDead()) {
z.render(gc, sbg, g);
z.findPrey();
}
}
}
So, basically my question is how - do I make my zombie character move?! Thanks for any help or guidance
Upvotes: 0
Views: 219
Reputation:
You should first move the zombie, then render it.
There's another problem: you allow to move the zombie all the way it should with one findPrey()
(using updatePosition()
in code below) method call.
Zombie class:
public void updatePosition() {
Pathfinder pf = new Pathfinder();
pf.setStartNode(xPosition,yPosition);
PathCoordinates p = pf.calculatePath()[0];
if(p.getXPosition() > xPosition) {
moveRight(2);
} if(p.getXPosition() < xPosition) {
moveLeft(2);
} if(p.getYPosition() > yPosition) {
moveUp(2);
} if(p.getYPosition() < yPosition) {
moveDown(2);
}
}
World class:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
for(Zombie z : zombies) {
if(!(z.isDead())) {
z.updatePosition();
z.render(gc, sbg, g);
}
}
}
Upvotes: 0