This site requires JavaScript, please enable it in your browser!
Greenfoot back
BroTolo
BroTolo wrote ...

2015/10/28

Zombie chasing the player while staying on the x-axis, only moving left and right.

BroTolo BroTolo

2015/10/28

#
I need the zombie which is spawned on the left and right of the character, but I'm not sure on how to make the zombie go to the right and left depending on the where the character is. Currently the zombie just: (placeholder) move(-1); He comes from the right side of the screen, but if it were to be spawned on the left side it would run into the wall. Also if i were to jump over the zombies approaching from the right, they would continue until they hit the wall on the left.
Super_Hippo Super_Hippo

2015/10/28

#
You could do it like this:
int y = getY();
Actor a = (Actor) getWorld().getObjects(Player.class).get(0);
turnTowards(a.getX(), a.getY());
move(1);
setLocation(getX(), y);
or
Actor a = (Actor) getWorld().getObjects(Player.class).get(0);
if (getX() > a.getX()) setLocation(getX()-1, getY());
else if (getX() < a.getX()) setLocation(getX()+1, getY());
BroTolo BroTolo

2015/10/28

#
Thank you kind sir
You need to login to post a reply.